From 92ab34b4a1918485ca51f5ae008fe74c188df6ec Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Mon, 6 May 2024 10:15:13 -0700 Subject: [PATCH 1/5] testing code in main.go --- .gitignore | 2 ++ go.mod | 2 ++ go.sum | 6 ++++ main.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 104 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index e2aa8a6..2d3837a 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ .vscode .idea test_config.json + +.DS_Store diff --git a/go.mod b/go.mod index eb1117f..b98fb6c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/DelineaXPM/tss-sdk-go/v2 go 1.13 + +require github.com/tidwall/gjson v1.17.1 // indirect diff --git a/go.sum b/go.sum index e69de29..a491a1d 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U= +github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= diff --git a/main.go b/main.go index 1a965f1..4c71158 100644 --- a/main.go +++ b/main.go @@ -2,32 +2,122 @@ package main import ( "fmt" + "encoding/json" "log" "os" "github.com/DelineaXPM/tss-sdk-go/v2/server" + + _"github.com/tidwall/gjson" ) +/* +TSS_USERNAME +TSS_PASSWORD +TSS_TENANT +TSS_SERVER_URL +TSS_TLD +*/ func main() { + os.Setenv("TSS_USERNAME", "dsvtest") + os.Setenv("TSS_PASSWORD", "testTEST1234!") + os.Setenv("TSS_SERVER_URL", "https://rasteamdev.qa.devsecretservercloud.com") + + + tss, err := server.New(server.Configuration{ Credentials: server.UserCredential{ Username: os.Getenv("TSS_USERNAME"), Password: os.Getenv("TSS_PASSWORD"), }, - Tenant: os.Getenv("TSS_TENANT"), +/* Tenant: os.Getenv("TSS_TENANT"), */ + ServerURL: os.Getenv("TSS_SERVER_URL"), + TLD: "com", }) if err != nil { log.Fatal("Error initializing the server configuration", err) } - s, err := tss.Secret(1) +// Get secret by ID +/* s, err := tss.Secret(49490) // Unix Account (SSH) password = Items.2.ItemValue */ +/* s, err := tss.Secret(49268) // Unix Account (SSH) password = Items.2.ItemValue */ +/* s, err := tss.Secret(51462) // DevOps Secrets Vault Client Credentials password = Items.2.ItemValue */ +/* s, err := tss.Secret(51463) // MySql Account password = Items.2.ItemValue */ +/* s, err := tss.Secret(51468) // Oracle Account password = Items.4.ItemValue */ +/* s, err := tss.Secret(51470) // SQL Server Account password = Items.2.ItemValue*/ +/* s, err := tss.Secret(51474) // SAP Account password = Items.2.ItemValue*/ +/* s, err := tss.Secret(51475) // Windows Account password = Items.2.ItemValue*/ +/* s, err := tss.Secret(53389) // Unix Account (SSH) password = Items.2.ItemValue */ + +/* s, err := tss.Secret(53974) // External-Secret (SSH) data = Items.0.ItemValue */ + +// Get secret by searchText and field + s, err := tss.Secrets("ESO-test-secret", "name") + +// Create new secret +/* + secretModel := new(server.Secret) + secretModel.Name = "Bill Test secret delete me" + secretModel.SiteID = 1 + secretModel.FolderID = 67 + secretModel.SecretTemplateID = 6007 + secretModel.AutoChangeEnabled = false + secretModel.Fields = make([]server.SecretField, 3) + secretModel.Fields[0].FieldID = 108 // machine + secretModel.Fields[0].ItemValue = "DSV TEST MACHINE" + secretModel.Fields[1].FieldID = 111 // username + secretModel.Fields[1].ItemValue = "dsv_username" + secretModel.Fields[2].FieldID = 110 // password + secretModel.Fields[2].ItemValue = "dsv_password" + + s, err := tss.CreateSecret(*secretModel) +*/ + +// Update a secret +/* + secretModel := new(server.Secret) + secretModel.ID = 49490 + secretModel.Name = "DSV update test secret" + secretModel.Fields = make([]server.SecretField, 1) + secretModel.Fields[0].FieldID = 110 // password + secretModel.Fields[0].ItemValue = "someNewPassword" + s, err := tss.UpdateSecret(*secretModel) +*/ + +// Delete Secret +/* + s := "" + err = tss.DeleteSecret(53392) +*/ + +/* + fmt.Printf("\n\n%+v \n\n", s) if err != nil { - log.Fatal("Error calling server.Secret", err) + log.Fatal("[Error]: ", err) + } +*/ + + jsonStr, err := json.Marshal(s) + if err != nil { + fmt.Println(err) + return + } + // extract key from secret using gjson +/* + val := gjson.Get(string(jsonStr), "Items.0.ItemValue") + if !val.Exists() { + fmt.Printf("property = %s ... %s ", val) + return } +*/ + fmt.Println(string(jsonStr)) + +/* if pw, ok := s.Field("password"); ok { - fmt.Print("the password is", pw) + fmt.Print("\n\nthe password is ", pw, "\n\n") } +*/ } From 73c9b0e6ab42a3ebaa7c19b5123c37d927f76661 Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Thu, 9 May 2024 12:44:55 -0700 Subject: [PATCH 2/5] Added apiVersion to SS api calls, fixed linters and fixed unit tests Signed-off-by: Bill Hamilton --- .gitignore | 1 + go.mod | 9 ++- main.go | 100 ++------------------------------- server/http.go | 4 +- server/secret.go | 15 ++--- server/secret_template.go | 4 +- server/secret_template_test.go | 2 +- server/secret_test.go | 56 +++++++++++------- server/server.go | 23 +++++--- 9 files changed, 76 insertions(+), 138 deletions(-) diff --git a/.gitignore b/.gitignore index 2d3837a..648db03 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ test_config.json .DS_Store +bh_test.go diff --git a/go.mod b/go.mod index b98fb6c..319448b 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,10 @@ module github.com/DelineaXPM/tss-sdk-go/v2 -go 1.13 +go 1.22 -require github.com/tidwall/gjson v1.17.1 // indirect +require github.com/tidwall/gjson v1.17.1 + +require ( + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect +) diff --git a/main.go b/main.go index 4c71158..318256a 100644 --- a/main.go +++ b/main.go @@ -2,122 +2,32 @@ package main import ( "fmt" - "encoding/json" "log" "os" "github.com/DelineaXPM/tss-sdk-go/v2/server" - - _"github.com/tidwall/gjson" ) -/* -TSS_USERNAME -TSS_PASSWORD -TSS_TENANT -TSS_SERVER_URL -TSS_TLD -*/ func main() { - os.Setenv("TSS_USERNAME", "dsvtest") - os.Setenv("TSS_PASSWORD", "testTEST1234!") - os.Setenv("TSS_SERVER_URL", "https://rasteamdev.qa.devsecretservercloud.com") - - - tss, err := server.New(server.Configuration{ Credentials: server.UserCredential{ Username: os.Getenv("TSS_USERNAME"), Password: os.Getenv("TSS_PASSWORD"), }, -/* Tenant: os.Getenv("TSS_TENANT"), */ - ServerURL: os.Getenv("TSS_SERVER_URL"), - TLD: "com", + Tenant: os.Getenv("TSS_TENANT"), }) if err != nil { log.Fatal("Error initializing the server configuration", err) } -// Get secret by ID -/* s, err := tss.Secret(49490) // Unix Account (SSH) password = Items.2.ItemValue */ -/* s, err := tss.Secret(49268) // Unix Account (SSH) password = Items.2.ItemValue */ -/* s, err := tss.Secret(51462) // DevOps Secrets Vault Client Credentials password = Items.2.ItemValue */ -/* s, err := tss.Secret(51463) // MySql Account password = Items.2.ItemValue */ -/* s, err := tss.Secret(51468) // Oracle Account password = Items.4.ItemValue */ -/* s, err := tss.Secret(51470) // SQL Server Account password = Items.2.ItemValue*/ -/* s, err := tss.Secret(51474) // SAP Account password = Items.2.ItemValue*/ -/* s, err := tss.Secret(51475) // Windows Account password = Items.2.ItemValue*/ -/* s, err := tss.Secret(53389) // Unix Account (SSH) password = Items.2.ItemValue */ - -/* s, err := tss.Secret(53974) // External-Secret (SSH) data = Items.0.ItemValue */ - -// Get secret by searchText and field - s, err := tss.Secrets("ESO-test-secret", "name") - -// Create new secret -/* - secretModel := new(server.Secret) - secretModel.Name = "Bill Test secret delete me" - secretModel.SiteID = 1 - secretModel.FolderID = 67 - secretModel.SecretTemplateID = 6007 - secretModel.AutoChangeEnabled = false - secretModel.Fields = make([]server.SecretField, 3) - secretModel.Fields[0].FieldID = 108 // machine - secretModel.Fields[0].ItemValue = "DSV TEST MACHINE" - secretModel.Fields[1].FieldID = 111 // username - secretModel.Fields[1].ItemValue = "dsv_username" - secretModel.Fields[2].FieldID = 110 // password - secretModel.Fields[2].ItemValue = "dsv_password" - - s, err := tss.CreateSecret(*secretModel) -*/ - -// Update a secret -/* - secretModel := new(server.Secret) - secretModel.ID = 49490 - secretModel.Name = "DSV update test secret" - secretModel.Fields = make([]server.SecretField, 1) - secretModel.Fields[0].FieldID = 110 // password - secretModel.Fields[0].ItemValue = "someNewPassword" - s, err := tss.UpdateSecret(*secretModel) -*/ + s, err := tss.Secret(1) - -// Delete Secret -/* - s := "" - err = tss.DeleteSecret(53392) -*/ - -/* - fmt.Printf("\n\n%+v \n\n", s) if err != nil { - log.Fatal("[Error]: ", err) - } -*/ - - jsonStr, err := json.Marshal(s) - if err != nil { - fmt.Println(err) - return - } - // extract key from secret using gjson -/* - val := gjson.Get(string(jsonStr), "Items.0.ItemValue") - if !val.Exists() { - fmt.Printf("property = %s ... %s ", val) - return + log.Fatal("Error calling server.Secret", err) } -*/ - fmt.Println(string(jsonStr)) - -/* if pw, ok := s.Field("password"); ok { - fmt.Print("\n\nthe password is ", pw, "\n\n") + fmt.Print("the password is", pw) } -*/ -} +} \ No newline at end of file diff --git a/server/http.go b/server/http.go index 73d0249..0872a4c 100644 --- a/server/http.go +++ b/server/http.go @@ -2,7 +2,7 @@ package server import ( "fmt" - "io/ioutil" + "io" "net/http" ) @@ -14,7 +14,7 @@ func handleResponse(res *http.Response, err error) ([]byte, *http.Response, erro return nil, res, err } - data, err := ioutil.ReadAll(res.Body) + data, err := io.ReadAll(res.Body) if err != nil { return nil, res, err diff --git a/server/secret.go b/server/secret.go index 01287ff..1071f41 100644 --- a/server/secret.go +++ b/server/secret.go @@ -50,7 +50,7 @@ type SshKeyArgs struct { func (s Server) Secret(id int) (*Secret, error) { secret := new(Secret) - if data, err := s.accessResource("GET", resource, strconv.Itoa(id), nil); err == nil { + if data, err := s.accessResource("GET", resource, strconv.Itoa(id), "v2/", nil); err == nil { if err = json.Unmarshal(data, secret); err != nil { log.Printf("[ERROR] error parsing response from /%s/%d: %q", resource, id, data) return nil, err @@ -65,7 +65,7 @@ func (s Server) Secret(id int) (*Secret, error) { if element.IsFile && element.FileAttachmentID != 0 && element.Filename != "" { path := fmt.Sprintf("%d/fields/%s", id, element.Slug) - if data, err := s.accessResource("GET", resource, path, nil); err == nil { + if data, err := s.accessResource("GET", resource, path, "v1/", nil); err == nil { secret.Fields[index].ItemValue = string(data) } else { return nil, err @@ -79,7 +79,7 @@ func (s Server) Secret(id int) (*Secret, error) { // Secret gets the secret with id from the Secret Server of the given tenant func (s Server) Secrets(searchText, field string) ([]Secret, error) { searchResult := new(SearchResult) - if data, err := s.searchResources(resource, searchText, field); err == nil { + if data, err := s.searchResources(resource, searchText, field, "v1/"); err == nil { if err = json.Unmarshal(data, searchResult); err != nil { log.Printf("[ERROR] error parsing response from /%s/%s: %q", resource, searchText, data) return nil, err @@ -135,8 +135,9 @@ func (s Server) writeSecret(secret Secret, method string, path string) (*Secret, // This SDK does support secret templates that accept both kinds // of file fields. fileFields := make([]SecretField, 0) - generalFields := make([]SecretField, 0) if secret.SshKeyArgs == nil || !secret.SshKeyArgs.GenerateSshKeys { + //nolint: ineffassign + var generalFields = make([]SecretField, 0) fileFields, generalFields, err = secret.separateFileFields(template) if err != nil { return nil, err @@ -162,7 +163,7 @@ func (s Server) writeSecret(secret Secret, method string, path string) (*Secret, secret.Fields = make([]SecretField, 0) } - if data, err := s.accessResource(method, resource, path, secret); err == nil { + if data, err := s.accessResource(method, resource, path, "v1/", secret); err == nil { if err = json.Unmarshal(data, writtenSecret); err != nil { log.Printf("[ERROR] error parsing response from /%s: %q", resource, data) return nil, err @@ -179,7 +180,7 @@ func (s Server) writeSecret(secret Secret, method string, path string) (*Secret, } func (s Server) DeleteSecret(id int) error { - _, err := s.accessResource("DELETE", resource, strconv.Itoa(id), nil) + _, err := s.accessResource("DELETE", resource, strconv.Itoa(id), "v1/", nil) return err } @@ -231,7 +232,7 @@ func (s Server) updateFiles(secretId int, fileFields []SecretField) error { if element.ItemValue == "" { path = fmt.Sprintf("%d/general", secretId) input = secretPatch{Data: fieldMods{SecretFields: []fieldMod{{Slug: element.Slug, Dirty: true, Value: nil}}}} - if _, err := s.accessResource("PATCH", resource, path, input); err != nil { + if _, err := s.accessResource("PATCH", resource, path, "v2/", input); err != nil { return err } } else { diff --git a/server/secret_template.go b/server/secret_template.go index e37ad3a..8066a47 100644 --- a/server/secret_template.go +++ b/server/secret_template.go @@ -28,7 +28,7 @@ type SecretTemplateField struct { func (s Server) SecretTemplate(id int) (*SecretTemplate, error) { secretTemplate := new(SecretTemplate) - if data, err := s.accessResource("GET", templateResource, strconv.Itoa(id), nil); err == nil { + if data, err := s.accessResource("GET", templateResource, strconv.Itoa(id), "v1/", nil); err == nil { if err = json.Unmarshal(data, secretTemplate); err != nil { log.Printf("[ERROR] error parsing response from /%s/%d: %q", templateResource, id, data) return nil, err @@ -52,7 +52,7 @@ func (s Server) GeneratePassword(slug string, template *SecretTemplate) (string, } path := fmt.Sprintf("generate-password/%d", fieldId) - if data, err := s.accessResource("POST", templateResource, path, nil); err == nil { + if data, err := s.accessResource("POST", templateResource, path, "v1/", nil); err == nil { passwordWithQuotes := string(data) return passwordWithQuotes[1 : len(passwordWithQuotes)-1], nil } else { diff --git a/server/secret_template_test.go b/server/secret_template_test.go index ef7738f..131db67 100644 --- a/server/secret_template_test.go +++ b/server/secret_template_test.go @@ -25,7 +25,7 @@ func TestSecretTemplate(t *testing.T) { return } - if template == nil { + if template == nil || template.Fields == nil { t.Error("secret data is nil") } diff --git a/server/secret_test.go b/server/secret_test.go index 8f28b50..a13bf68 100644 --- a/server/secret_test.go +++ b/server/secret_test.go @@ -2,7 +2,6 @@ package server import ( "encoding/json" - "io/ioutil" "os" "regexp" "strconv" @@ -29,16 +28,17 @@ func TestSecret(t *testing.T) { return } - if s == nil { - t.Error("secret data is nil") - } - - if _, ok := s.Field("password"); !ok { - t.Error("no password field") - } + if s != nil { + if _, ok := s.Field("password"); !ok { + t.Error("no password field") + } - if _, ok := s.Field("nonexistent"); ok { - t.Error("s.Field says nonexistent field exists") + if _, ok := s.Field("nonexistent"); ok { + t.Error("s.Field says nonexistent field exists") + } + } else { + t.Error("secret data is nil") + return } } @@ -63,6 +63,8 @@ func TestSecretCRUD(t *testing.T) { } fieldId := -1 + fieldIdUserName := -1 + fieldIdMachineName := -1 if siteId < 0 || folderId < 0 || templateId < 0 { return } @@ -78,6 +80,12 @@ func TestSecretCRUD(t *testing.T) { fieldId = field.SecretTemplateFieldID break } + if field.DisplayName == "Machine" { + fieldIdMachineName = field.SecretTemplateFieldID + } + if field.DisplayName == "Username" { + fieldIdUserName = field.SecretTemplateFieldID + } } if fieldId < 0 { t.Errorf("Unable to find a password field on the secret template with the given id '%d'", templateId) @@ -86,15 +94,21 @@ func TestSecretCRUD(t *testing.T) { t.Logf("Using field ID '%d' for the password field on the template with ID '%d'", fieldId, templateId) // Test creation of a new secret - refSecret := new(Secret) password := testPassword - refSecret.Name = "Test Secret" + refSecret := new(Secret) + refSecret.Name = "Secret Server Unit Test" refSecret.SiteID = siteId refSecret.FolderID = folderId refSecret.SecretTemplateID = templateId - refSecret.Fields = make([]SecretField, 1) - refSecret.Fields[0].FieldID = fieldId + refSecret.AutoChangeEnabled = false + refSecret.Fields = make([]SecretField, 3) + refSecret.Fields[0].FieldID = fieldId // password refSecret.Fields[0].ItemValue = password + refSecret.Fields[1].FieldID = fieldIdMachineName // machine + refSecret.Fields[1].ItemValue = "SS Test" + refSecret.Fields[2].FieldID = fieldIdUserName // username + refSecret.Fields[2].ItemValue = "ss_test_username" + sc, err := tss.CreateSecret(*refSecret) if err != nil { t.Error("calling server.CreateSecret:", err) @@ -184,14 +198,14 @@ func TestSecretCRUD(t *testing.T) { // Test the deletion of the new secret err = tss.DeleteSecret(sc.ID) if err != nil { - t.Error("calling server.DeleteSecret:", err) + t.Error("error calling server.DeleteSecret:", err) return } // Test read of the deleted secret fails s, err := tss.Secret(sc.ID) - if s != nil { - t.Errorf("deleted secret with id '%d' returned from read", sc.ID) + if s.Active || err != nil { + t.Errorf("deleted secret with id '%d' returned from read. S = %+v", sc.ID, s) } } @@ -559,8 +573,8 @@ func TestSecretCRUDForSSHTemplate(t *testing.T) { // Test read of the deleted secret fails s, err := tss.Secret(sc.ID) - if s != nil { - t.Errorf("deleted secret with id '%d' returned from read", sc.ID) + if s.Active || err != nil { + t.Errorf("deleted secret with id '%d' returned from read. error = %+v", sc.ID, err) } } @@ -614,10 +628,10 @@ func TestSearchWithoutField(t *testing.T) { func initServer() (*Server, error) { var config *Configuration - if cj, err := ioutil.ReadFile("../test_config.json"); err == nil { + if cj, err := os.ReadFile("../test_config.json"); err == nil { config = new(Configuration) - json.Unmarshal(cj, &config) + _ = json.Unmarshal(cj, &config) } else { config = &Configuration{ Credentials: UserCredential{ diff --git a/server/server.go b/server/server.go index 8ea18ac..1de57db 100644 --- a/server/server.go +++ b/server/server.go @@ -16,7 +16,7 @@ import ( const ( cloudBaseURLTemplate string = "https://%s.secretservercloud.%s/" - defaultAPIPathURI string = "/api/v1" + defaultAPIPathURI string = "/api/" defaultTokenPathURI string = "/oauth2/token" defaultTLD string = "com" ) @@ -29,9 +29,9 @@ type UserCredential struct { // Configuration settings for the API type Configuration struct { - Credentials UserCredential - ServerURL, TLD, Tenant, apiPathURI, tokenPathURI string - TLSClientConfig *tls.Config + Credentials UserCredential + ServerURL, TLD, Tenant, apiPathURI, apiVersion, tokenPathURI string + TLSClientConfig *tls.Config } // Server provides access to secrets stored in Delinea Secret Server @@ -77,9 +77,10 @@ func (s Server) urlFor(resource, path string) string { strings.Trim(baseURL, "/"), strings.Trim(s.tokenPathURI, "/")) default: - return fmt.Sprintf("%s/%s/%s/%s", + return fmt.Sprintf("%s/%s/%s/%s/%s", strings.Trim(baseURL, "/"), strings.Trim(s.apiPathURI, "/"), + strings.Trim(s.apiVersion, "/"), strings.Trim(resource, "/"), strings.Trim(path, "/")) } @@ -95,9 +96,10 @@ func (s Server) urlForSearch(resource, searchText, fieldName string) string { } switch { case resource == "secrets": - url := fmt.Sprintf("%s/%s/%s?paging.filter.searchText=%s&paging.filter.searchField=%s&paging.filter.doNotCalculateTotal=true&paging.take=30&&paging.skip=0", + url := fmt.Sprintf("%s/%s/%s/%s?paging.filter.searchText=%s&paging.filter.searchField=%s&paging.filter.doNotCalculateTotal=true&paging.take=30&&paging.skip=0", strings.Trim(baseURL, "/"), strings.Trim(s.apiPathURI, "/"), + strings.Trim(s.apiVersion, "/"), strings.Trim(resource, "/"), searchText, fieldName) @@ -112,7 +114,7 @@ func (s Server) urlForSearch(resource, searchText, fieldName string) string { // accessResource uses the accessToken to access the API resource. // It assumes an appropriate combination of method, resource, path and input. -func (s Server) accessResource(method, resource, path string, input interface{}) ([]byte, error) { +func (s Server) accessResource(method, resource, path, version string, input interface{}) ([]byte, error) { switch resource { case "secrets": case "secret-templates": @@ -134,6 +136,7 @@ func (s Server) accessResource(method, resource, path string, input interface{}) } } + s.apiVersion = version req, err := http.NewRequest(method, s.urlFor(resource, path), body) if err != nil { @@ -165,7 +168,7 @@ func (s Server) accessResource(method, resource, path string, input interface{}) // searchResources uses the accessToken to search for API resources. // It assumes an appropriate combination of resource, search text. // field is optional -func (s Server) searchResources(resource, searchText, field string) ([]byte, error) { +func (s Server) searchResources(resource, searchText, field, apiVersion string) ([]byte, error) { switch resource { case "secrets": default: @@ -178,6 +181,7 @@ func (s Server) searchResources(resource, searchText, field string) ([]byte, err method := "GET" body := bytes.NewBuffer([]byte{}) + s.apiVersion = apiVersion req, err := http.NewRequest(method, s.urlForSearch(resource, searchText, field), body) if err != nil { @@ -238,6 +242,7 @@ func (s Server) uploadFile(secretId int, fileField SecretField) error { return err } + s.apiVersion = "v1/" // Make the request req, err := http.NewRequest("PUT", s.urlFor(resource, path), body) if err != nil { @@ -263,6 +268,8 @@ func (s Server) getAccessToken() (string, error) { values["domain"] = []string{s.Credentials.Domain} } + s.apiVersion = "v2/" + body := strings.NewReader(values.Encode()) requestUrl := s.urlFor("token", "") data, _, err := handleResponse(http.Post(requestUrl, "application/x-www-form-urlencoded", body)) From 7ae94560a2069f0b874f106438eac51fc4528c4d Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Thu, 9 May 2024 13:18:15 -0700 Subject: [PATCH 3/5] fixed more conflicts Signed-off-by: Bill Hamilton --- server/server.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/server.go b/server/server.go index ca6e9f2..f71c476 100644 --- a/server/server.go +++ b/server/server.go @@ -152,7 +152,7 @@ func (s Server) accessResource(method, resource, path, version string, input int return nil, err } - req, err := http.NewRequest(method, s.urlFor(resource, path), body) + req, err = http.NewRequest(method, s.urlFor(resource, path), body) if err != nil { log.Printf("[ERROR] creating req: %s /%s/%s: %s", method, resource, path, err) @@ -204,7 +204,7 @@ func (s Server) searchResources(resource, searchText, field, apiVersion string) return nil, err } - req, err := http.NewRequest(method, s.urlForSearch(resource, searchText, field), body) + req, err = http.NewRequest(method, s.urlForSearch(resource, searchText, field), body) if err != nil { log.Printf("[ERROR] creating req: %s /%s/%s/%s: %s", method, resource, searchText, field, err) @@ -285,7 +285,7 @@ func (s *Server) getAccessToken() (string, error) { body := strings.NewReader(values.Encode()) requestUrl := s.urlFor("token", "") - data, _, err := handleResponse(http.Post(requestUrl, "application/x-www-form-urlencoded", body)) + _, _, err := handleResponse(http.Post(requestUrl, "application/x-www-form-urlencoded", body)) response, err := s.checkPlatformDetails() From c8895f63607965277303d985e1abe805430008fb Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Fri, 10 May 2024 10:47:45 -0700 Subject: [PATCH 4/5] fixed test conflicts Signed-off-by: Bill Hamilton --- .gitignore | 2 +- server/http.go | 4 ++-- server/secret_test.go | 27 ++++++++++----------------- server/server.go | 40 ++++++---------------------------------- 4 files changed, 19 insertions(+), 54 deletions(-) diff --git a/.gitignore b/.gitignore index 648db03..e89e845 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,4 @@ test_config.json .DS_Store -bh_test.go +bh_example.go diff --git a/server/http.go b/server/http.go index 0872a4c..73d0249 100644 --- a/server/http.go +++ b/server/http.go @@ -2,7 +2,7 @@ package server import ( "fmt" - "io" + "io/ioutil" "net/http" ) @@ -14,7 +14,7 @@ func handleResponse(res *http.Response, err error) ([]byte, *http.Response, erro return nil, res, err } - data, err := io.ReadAll(res.Body) + data, err := ioutil.ReadAll(res.Body) if err != nil { return nil, res, err diff --git a/server/secret_test.go b/server/secret_test.go index 0e1be10..137bc5f 100644 --- a/server/secret_test.go +++ b/server/secret_test.go @@ -38,7 +38,7 @@ func GetSecret(t *testing.T, tss *Server) { s, err := tss.Secret(id) if err != nil { - t.Error("calling server.Secret:", err) + t.Errorf("calling server.Secret: err = %+v, config = %+v, secretID = %d", err, tss, id) return } @@ -231,13 +231,8 @@ func SecretCRUD(t *testing.T, tss *Server) { // Test read of the deleted secret fails s, err := tss.Secret(sc.ID) -<<<<<<< HEAD - if s.Active || err != nil { - t.Errorf("deleted secret with id '%d' returned from read. S = %+v", sc.ID, s) -======= - if s != nil && s.Active { - t.Errorf("deleted secret with id '%d' returned from read", sc.ID) ->>>>>>> main + if s != nil && s.Active || err != nil { + t.Errorf("deleted secret with id '%d' returned from read, err = %+v", sc.ID, err) } } @@ -618,13 +613,8 @@ func SecretCRUDForSSHTemplate(t *testing.T, tss *Server) { // Test read of the deleted secret fails s, err := tss.Secret(sc.ID) -<<<<<<< HEAD - if s.Active || err != nil { - t.Errorf("deleted secret with id '%d' returned from read. error = %+v", sc.ID, err) -======= - if s != nil && s.Active { - t.Errorf("deleted secret with id '%d' returned from read", sc.ID) ->>>>>>> main + if s != nil && s.Active || err != nil { + t.Errorf("deleted secret with id '%d' returned from read. err = %+v", sc.ID, err) } } @@ -729,10 +719,13 @@ func initServer() (*Server, error) { func initPlatformServer() (*Server, error) { var config *Configuration - if cj, err := ioutil.ReadFile("../test_config.json"); err == nil { + if cj, err := os.ReadFile("../test_config.json"); err == nil { config = new(Configuration) - json.Unmarshal(cj, &config) + err := json.Unmarshal(cj, &config) + if err != nil { + return nil, err + } } else { config = &Configuration{ Credentials: UserCredential{ diff --git a/server/server.go b/server/server.go index f71c476..c8b1fa1 100644 --- a/server/server.go +++ b/server/server.go @@ -137,23 +137,14 @@ func (s Server) accessResource(method, resource, path, version string, input int } } - s.apiVersion = version - req, err := http.NewRequest(method, s.urlFor(resource, path), body) - - if err != nil { - log.Printf("[ERROR] creating req: %s /%s/%s: %s", method, resource, path, err) - return nil, err - } - accessToken, err := s.getAccessToken() - if err != nil { log.Print("[ERROR] error getting accessToken:", err) return nil, err } - req, err = http.NewRequest(method, s.urlFor(resource, path), body) - + s.apiVersion = version + req, err := http.NewRequest(method, s.urlFor(resource, path), body) if err != nil { log.Printf("[ERROR] creating req: %s /%s/%s: %s", method, resource, path, err) return nil, err @@ -186,17 +177,6 @@ func (s Server) searchResources(resource, searchText, field, apiVersion string) return nil, fmt.Errorf(message) } - method := "GET" - body := bytes.NewBuffer([]byte{}) - - s.apiVersion = apiVersion - req, err := http.NewRequest(method, s.urlForSearch(resource, searchText, field), body) - - if err != nil { - log.Printf("[ERROR] creating req: %s /%s/%s/%s: %s", method, resource, searchText, field, err) - return nil, err - } - accessToken, err := s.getAccessToken() if err != nil { @@ -204,7 +184,10 @@ func (s Server) searchResources(resource, searchText, field, apiVersion string) return nil, err } - req, err = http.NewRequest(method, s.urlForSearch(resource, searchText, field), body) + method := "GET" + body := bytes.NewBuffer([]byte{}) + s.apiVersion = apiVersion + req, err := http.NewRequest(method, s.urlForSearch(resource, searchText, field), body) if err != nil { log.Printf("[ERROR] creating req: %s /%s/%s/%s: %s", method, resource, searchText, field, err) @@ -277,18 +260,7 @@ func (s *Server) getAccessToken() (string, error) { if s.Credentials.Token != "" { return s.Credentials.Token, nil } - if s.Credentials.Domain != "" { - values["domain"] = []string{s.Credentials.Domain} - } - - s.apiVersion = "v2/" - - body := strings.NewReader(values.Encode()) - requestUrl := s.urlFor("token", "") - _, _, err := handleResponse(http.Post(requestUrl, "application/x-www-form-urlencoded", body)) - response, err := s.checkPlatformDetails() - if err != nil { log.Print("Error while checking server details:", err) return "", err From db5330467fd57702ab686ec0d1a9b443add523db Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Fri, 10 May 2024 10:50:55 -0700 Subject: [PATCH 5/5] one more test fix Signed-off-by: Bill Hamilton --- server/secret_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/secret_test.go b/server/secret_test.go index 137bc5f..47ea406 100644 --- a/server/secret_test.go +++ b/server/secret_test.go @@ -2,6 +2,7 @@ package server import ( "encoding/json" + "io/ioutil" "os" "regexp" "strconv" @@ -698,7 +699,7 @@ func SearchWithoutField(t *testing.T, tss *Server) { func initServer() (*Server, error) { var config *Configuration - if cj, err := os.ReadFile("../test_config.json"); err == nil { + if cj, err := ioutil.ReadFile("../test_config.json"); err == nil { config = new(Configuration) _ = json.Unmarshal(cj, &config) @@ -719,7 +720,7 @@ func initServer() (*Server, error) { func initPlatformServer() (*Server, error) { var config *Configuration - if cj, err := os.ReadFile("../test_config.json"); err == nil { + if cj, err := ioutil.ReadFile("../test_config.json"); err == nil { config = new(Configuration) err := json.Unmarshal(cj, &config)