diff --git a/api/managed_account/managed_account.go b/api/managed_account/managed_account.go index dafd8da..f80465f 100644 --- a/api/managed_account/managed_account.go +++ b/api/managed_account/managed_account.go @@ -26,31 +26,31 @@ type ManagedAccountstObj struct { // NewManagedAccountObj creates managed account obj func NewManagedAccountObj(authentication authentication.AuthenticationObj, logger logging.Logger) (*ManagedAccountstObj, error) { - managedAccounObj := &ManagedAccountstObj{ + managedAccountObj := &ManagedAccountstObj{ log: logger, authenticationObj: authentication, } - return managedAccounObj, nil + return managedAccountObj, nil } // GetSecrets is responsible for getting a list of managed account secret values based on the list of systems and account names. -func (managedAccounObj *ManagedAccountstObj) GetSecrets(secretPaths []string, separator string) (map[string]string, error) { - return managedAccounObj.ManageAccountFlow(secretPaths, separator) +func (managedAccountObj *ManagedAccountstObj) GetSecrets(secretPaths []string, separator string) (map[string]string, error) { + return managedAccountObj.ManageAccountFlow(secretPaths, separator) } // GetSecret returns secret value for a specific System Name and Account Name. -func (managedAccounObj *ManagedAccountstObj) GetSecret(secretPath string, separator string) (string, error) { +func (managedAccountObj *ManagedAccountstObj) GetSecret(secretPath string, separator string) (string, error) { managedAccountList := []string{} - secrets, err := managedAccounObj.ManageAccountFlow(append(managedAccountList, secretPath), separator) + secrets, err := managedAccountObj.ManageAccountFlow(append(managedAccountList, secretPath), separator) secretValue := secrets[secretPath] return secretValue, err } // ManageAccountFlow is responsible for creating a dictionary of managed account system/name and secret key-value pairs. -func (managedAccounObj *ManagedAccountstObj) ManageAccountFlow(secretsToRetrieve []string, separator string) (map[string]string, error) { +func (managedAccountObj *ManagedAccountstObj) ManageAccountFlow(secretsToRetrieve []string, separator string) (map[string]string, error) { - secretsToRetrieve = utils.ValidatePaths(secretsToRetrieve, true, separator, managedAccounObj.log) - managedAccounObj.log.Info(fmt.Sprintf("Retrieving %v Secrets", len(secretsToRetrieve))) + secretsToRetrieve = utils.ValidatePaths(secretsToRetrieve, true, separator, managedAccountObj.log) + managedAccountObj.log.Info(fmt.Sprintf("Retrieving %v Secrets", len(secretsToRetrieve))) secretDictionary := make(map[string]string) var saveLastErr error = nil @@ -65,36 +65,36 @@ func (managedAccounObj *ManagedAccountstObj) ManageAccountFlow(secretsToRetrieve var err error - ManagedAccountGetUrl := managedAccounObj.authenticationObj.ApiUrl.JoinPath("ManagedAccounts").String() + "?" + v.Encode() - managedAccount, err := managedAccounObj.ManagedAccountGet(systemName, accountName, ManagedAccountGetUrl) + ManagedAccountGetUrl := managedAccountObj.authenticationObj.ApiUrl.JoinPath("ManagedAccounts").String() + "?" + v.Encode() + managedAccount, err := managedAccountObj.ManagedAccountGet(systemName, accountName, ManagedAccountGetUrl) if err != nil { saveLastErr = err - managedAccounObj.log.Error(fmt.Sprintf("%v secretsPath: %v %v %v", err.Error(), systemName, separator, accountName)) + managedAccountObj.log.Error(fmt.Sprintf("%v secretsPath: %v %v %v", err.Error(), systemName, separator, accountName)) continue } - ManagedAccountCreateRequestUrl := managedAccounObj.authenticationObj.ApiUrl.JoinPath("Requests").String() - requestId, err := managedAccounObj.ManagedAccountCreateRequest(managedAccount.SystemId, managedAccount.AccountId, ManagedAccountCreateRequestUrl) + ManagedAccountCreateRequestUrl := managedAccountObj.authenticationObj.ApiUrl.JoinPath("Requests").String() + requestId, err := managedAccountObj.ManagedAccountCreateRequest(managedAccount.SystemId, managedAccount.AccountId, ManagedAccountCreateRequestUrl) if err != nil { saveLastErr = err - managedAccounObj.log.Error(fmt.Sprintf("%v secretsPath: %v %v %v", err.Error(), systemName, separator, accountName)) + managedAccountObj.log.Error(fmt.Sprintf("%v secretsPath: %v %v %v", err.Error(), systemName, separator, accountName)) continue } - CredentialByRequestIdUrl := managedAccounObj.authenticationObj.ApiUrl.JoinPath("Credentials", requestId).String() - secret, err := managedAccounObj.CredentialByRequestId(requestId, CredentialByRequestIdUrl) + CredentialByRequestIdUrl := managedAccountObj.authenticationObj.ApiUrl.JoinPath("Credentials", requestId).String() + secret, err := managedAccountObj.CredentialByRequestId(requestId, CredentialByRequestIdUrl) if err != nil { saveLastErr = err - managedAccounObj.log.Error(fmt.Sprintf("%v secretsPath: %v %v %v", err.Error(), systemName, separator, accountName)) + managedAccountObj.log.Error(fmt.Sprintf("%v secretsPath: %v %v %v", err.Error(), systemName, separator, accountName)) continue } - ManagedAccountRequestCheckInUrl := managedAccounObj.authenticationObj.ApiUrl.JoinPath("Requests", requestId, "checkin").String() - _, err = managedAccounObj.ManagedAccountRequestCheckIn(requestId, ManagedAccountRequestCheckInUrl) + ManagedAccountRequestCheckInUrl := managedAccountObj.authenticationObj.ApiUrl.JoinPath("Requests", requestId, "checkin").String() + _, err = managedAccountObj.ManagedAccountRequestCheckIn(requestId, ManagedAccountRequestCheckInUrl) if err != nil { saveLastErr = err - managedAccounObj.log.Error(fmt.Sprintf("%v secretsPath: %v %v %v", err.Error(), systemName, separator, accountName)) + managedAccountObj.log.Error(fmt.Sprintf("%v secretsPath: %v %v %v", err.Error(), systemName, separator, accountName)) continue } @@ -107,22 +107,22 @@ func (managedAccounObj *ManagedAccountstObj) ManageAccountFlow(secretsToRetrieve } // ManagedAccountGet is responsible for retrieving a managed account secret based on the system and name. -func (managedAccounObj *ManagedAccountstObj) ManagedAccountGet(systemName string, accountName string, url string) (entities.ManagedAccount, error) { +func (managedAccountObj *ManagedAccountstObj) ManagedAccountGet(systemName string, accountName string, url string) (entities.ManagedAccount, error) { messageLog := fmt.Sprintf("%v %v", "GET", url) - managedAccounObj.log.Debug(messageLog) + managedAccountObj.log.Debug(messageLog) var body io.ReadCloser var technicalError error var businessError error technicalError = backoff.Retry(func() error { - body, _, technicalError, businessError = managedAccounObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "GET", bytes.Buffer{}, "ManagedAccountGet", "", "") + body, _, technicalError, businessError = managedAccountObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "GET", bytes.Buffer{}, "ManagedAccountGet", "", "") if technicalError != nil { return technicalError } return nil - }, managedAccounObj.authenticationObj.ExponentialBackOff) + }, managedAccountObj.authenticationObj.ExponentialBackOff) if technicalError != nil { return entities.ManagedAccount{}, technicalError @@ -142,7 +142,7 @@ func (managedAccounObj *ManagedAccountstObj) ManagedAccountGet(systemName string var managedAccountObject entities.ManagedAccount err = json.Unmarshal(bodyBytes, &managedAccountObject) if err != nil { - managedAccounObj.log.Error(err.Error()) + managedAccountObj.log.Error(err.Error()) return entities.ManagedAccount{}, err } @@ -151,9 +151,9 @@ func (managedAccounObj *ManagedAccountstObj) ManagedAccountGet(systemName string } // ManagedAccountCreateRequest calls Secret Safe API Requests enpoint and returns a request Id as string. -func (managedAccounObj *ManagedAccountstObj) ManagedAccountCreateRequest(systemName int, accountName int, url string) (string, error) { +func (managedAccountObj *ManagedAccountstObj) ManagedAccountCreateRequest(systemName int, accountName int, url string) (string, error) { messageLog := fmt.Sprintf("%v %v", "POST", url) - managedAccounObj.log.Debug(messageLog) + managedAccountObj.log.Debug(messageLog) data := fmt.Sprintf(`{"SystemID":%v, "AccountID":%v, "DurationMinutes":5, "Reason":"Tesr", "ConflictOption": "reuse"}`, systemName, accountName) b := bytes.NewBufferString(data) @@ -163,9 +163,9 @@ func (managedAccounObj *ManagedAccountstObj) ManagedAccountCreateRequest(systemN var businessError error technicalError = backoff.Retry(func() error { - body, _, technicalError, businessError = managedAccounObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "POST", *b, "ManagedAccountCreateRequest", "", "") + body, _, technicalError, businessError = managedAccountObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "POST", *b, "ManagedAccountCreateRequest", "", "") return technicalError - }, managedAccounObj.authenticationObj.ExponentialBackOff) + }, managedAccountObj.authenticationObj.ExponentialBackOff) if technicalError != nil { return "", technicalError @@ -190,18 +190,18 @@ func (managedAccounObj *ManagedAccountstObj) ManagedAccountCreateRequest(systemN // CredentialByRequestId calls Secret Safe API Credentials/ // enpoint and returns secret value by request Id. -func (managedAccounObj *ManagedAccountstObj) CredentialByRequestId(requestId string, url string) (string, error) { +func (managedAccountObj *ManagedAccountstObj) CredentialByRequestId(requestId string, url string) (string, error) { messageLog := fmt.Sprintf("%v %v", "GET", url) - managedAccounObj.log.Debug(strings.Replace(messageLog, requestId, "****", -1)) + managedAccountObj.log.Debug(strings.Replace(messageLog, requestId, "****", -1)) var body io.ReadCloser var technicalError error var businessError error technicalError = backoff.Retry(func() error { - body, _, technicalError, businessError = managedAccounObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "GET", bytes.Buffer{}, "CredentialByRequestId", "", "") + body, _, technicalError, businessError = managedAccountObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "GET", bytes.Buffer{}, "CredentialByRequestId", "", "") return technicalError - }, managedAccounObj.authenticationObj.ExponentialBackOff) + }, managedAccountObj.authenticationObj.ExponentialBackOff) if technicalError != nil { return "", technicalError @@ -214,7 +214,7 @@ func (managedAccounObj *ManagedAccountstObj) CredentialByRequestId(requestId str defer body.Close() bodyBytes, err := io.ReadAll(body) if err != nil { - managedAccounObj.log.Error(err.Error()) + managedAccountObj.log.Error(err.Error()) return "", err } @@ -224,9 +224,9 @@ func (managedAccounObj *ManagedAccountstObj) CredentialByRequestId(requestId str } // ManagedAccountRequestCheckIn calls Secret Safe API "Requests//checkin enpoint. -func (managedAccounObj *ManagedAccountstObj) ManagedAccountRequestCheckIn(requestId string, url string) (string, error) { +func (managedAccountObj *ManagedAccountstObj) ManagedAccountRequestCheckIn(requestId string, url string) (string, error) { messageLog := fmt.Sprintf("%v %v", "PUT", url) - managedAccounObj.log.Debug(strings.Replace(messageLog, requestId, "****", -1)) + managedAccountObj.log.Debug(strings.Replace(messageLog, requestId, "****", -1)) data := "{}" b := bytes.NewBufferString(data) @@ -235,9 +235,9 @@ func (managedAccounObj *ManagedAccountstObj) ManagedAccountRequestCheckIn(reques var businessError error technicalError = backoff.Retry(func() error { - _, _, technicalError, businessError = managedAccounObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "PUT", *b, "ManagedAccountRequestCheckIn", "", "") + _, _, technicalError, businessError = managedAccountObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "PUT", *b, "ManagedAccountRequestCheckIn", "", "") return technicalError - }, managedAccounObj.authenticationObj.ExponentialBackOff) + }, managedAccountObj.authenticationObj.ExponentialBackOff) if technicalError != nil { return "", technicalError @@ -251,7 +251,7 @@ func (managedAccounObj *ManagedAccountstObj) ManagedAccountRequestCheckIn(reques } // ManageAccountCreateFlow is responsible for creating a managed accounts in Password Safe. -func (managedAccounObj *ManagedAccountstObj) ManageAccountCreateFlow(systemNameTarget string, accountDetails entities.AccountDetails) (entities.CreateManagedAccountsResponse, error) { +func (managedAccountObj *ManagedAccountstObj) ManageAccountCreateFlow(systemNameTarget string, accountDetails entities.AccountDetails) (entities.CreateManagedAccountsResponse, error) { var managedSystem *entities.ManagedSystemResponse var createResponse entities.CreateManagedAccountsResponse @@ -262,8 +262,8 @@ func (managedAccounObj *ManagedAccountstObj) ManageAccountCreateFlow(systemNameT return createResponse, err } - ManagedAccountSytemUrl := managedAccounObj.authenticationObj.ApiUrl.JoinPath("ManagedSystems").String() - managedSystemGetSystemsResponse, err := managedAccounObj.ManagedSystemGetSystems(ManagedAccountSytemUrl) + ManagedAccountSystemUrl := managedAccountObj.authenticationObj.ApiUrl.JoinPath("ManagedSystems").String() + managedSystemGetSystemsResponse, err := managedAccountObj.ManagedSystemGetSystems(ManagedAccountSystemUrl) if err != nil { return createResponse, err @@ -280,8 +280,8 @@ func (managedAccounObj *ManagedAccountstObj) ManageAccountCreateFlow(systemNameT return createResponse, fmt.Errorf("managed system %v was not found in managed system list", systemNameTarget) } - ManagedAccountCreateManagedAccountUrl := managedAccounObj.authenticationObj.ApiUrl.JoinPath("ManagedSystems", fmt.Sprintf("%d", managedSystem.ManagedSystemID), "ManagedAccounts").String() - createResponse, err = managedAccounObj.ManagedAccountCreateManagedAccount(accountDetails, ManagedAccountCreateManagedAccountUrl) + ManagedAccountCreateManagedAccountUrl := managedAccountObj.authenticationObj.ApiUrl.JoinPath("ManagedSystems", fmt.Sprintf("%d", managedSystem.ManagedSystemID), "ManagedAccounts").String() + createResponse, err = managedAccountObj.ManagedAccountCreateManagedAccount(accountDetails, ManagedAccountCreateManagedAccountUrl) if err != nil { return createResponse, err @@ -292,9 +292,9 @@ func (managedAccounObj *ManagedAccountstObj) ManageAccountCreateFlow(systemNameT } // ManagedAccountCreateManagedAccount calls Secret Safe API Requests enpoint to create managed accounts. -func (managedAccounObj *ManagedAccountstObj) ManagedAccountCreateManagedAccount(accountDetails entities.AccountDetails, url string) (entities.CreateManagedAccountsResponse, error) { +func (managedAccountObj *ManagedAccountstObj) ManagedAccountCreateManagedAccount(accountDetails entities.AccountDetails, url string) (entities.CreateManagedAccountsResponse, error) { messageLog := fmt.Sprintf("%v %v", "POST", url) - managedAccounObj.log.Debug(messageLog) + managedAccountObj.log.Debug(messageLog) accountDetailsJson, err := json.Marshal(accountDetails) if err != nil { @@ -310,9 +310,9 @@ func (managedAccounObj *ManagedAccountstObj) ManagedAccountCreateManagedAccount( var businessError error technicalError = backoff.Retry(func() error { - body, _, technicalError, businessError = managedAccounObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "POST", *b, "ManagedAccountCreateManagedAccount", "", "") + body, _, technicalError, businessError = managedAccountObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "POST", *b, "ManagedAccountCreateManagedAccount", "", "") return technicalError - }, managedAccounObj.authenticationObj.ExponentialBackOff) + }, managedAccountObj.authenticationObj.ExponentialBackOff) var CreateManagedAccountsResponse entities.CreateManagedAccountsResponse @@ -334,7 +334,7 @@ func (managedAccounObj *ManagedAccountstObj) ManagedAccountCreateManagedAccount( err = json.Unmarshal([]byte(bodyBytes), &CreateManagedAccountsResponse) if err != nil { - managedAccounObj.log.Error(err.Error()) + managedAccountObj.log.Error(err.Error()) return entities.CreateManagedAccountsResponse{}, err } @@ -343,22 +343,22 @@ func (managedAccounObj *ManagedAccountstObj) ManagedAccountCreateManagedAccount( } // ManagedAccountGetSystem is responsible for retrieving managed systems list -func (managedAccounObj *ManagedAccountstObj) ManagedSystemGetSystems(url string) ([]entities.ManagedSystemResponse, error) { +func (managedAccountObj *ManagedAccountstObj) ManagedSystemGetSystems(url string) ([]entities.ManagedSystemResponse, error) { messageLog := fmt.Sprintf("%v %v", "GET", url) - managedAccounObj.log.Debug(messageLog) + managedAccountObj.log.Debug(messageLog) var body io.ReadCloser var technicalError error var businessError error technicalError = backoff.Retry(func() error { - body, _, technicalError, businessError = managedAccounObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "GET", bytes.Buffer{}, "ManagedSystemGetSystems", "", "") + body, _, technicalError, businessError = managedAccountObj.authenticationObj.HttpClient.CallSecretSafeAPI(url, "GET", bytes.Buffer{}, "ManagedSystemGetSystems", "", "") if technicalError != nil { return technicalError } return nil - }, managedAccounObj.authenticationObj.ExponentialBackOff) + }, managedAccountObj.authenticationObj.ExponentialBackOff) var managedSystemObject []entities.ManagedSystemResponse @@ -379,7 +379,7 @@ func (managedAccounObj *ManagedAccountstObj) ManagedSystemGetSystems(url string) err = json.Unmarshal(bodyBytes, &managedSystemObject) if err != nil { - managedAccounObj.log.Error(err.Error()) + managedAccountObj.log.Error(err.Error()) return managedSystemObject, err }