Skip to content

Commit

Permalink
fix: solve PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
btfhernandez committed Nov 7, 2024
1 parent e860d85 commit 8a8b40f
Showing 1 changed file with 54 additions and 54 deletions.
108 changes: 54 additions & 54 deletions api/managed_account/managed_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -190,18 +190,18 @@ func (managedAccounObj *ManagedAccountstObj) ManagedAccountCreateRequest(systemN

// CredentialByRequestId calls Secret Safe API Credentials/<request_id>
// 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
Expand All @@ -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
}

Expand All @@ -224,9 +224,9 @@ func (managedAccounObj *ManagedAccountstObj) CredentialByRequestId(requestId str
}

// ManagedAccountRequestCheckIn calls Secret Safe API "Requests/<request_id>/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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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

Expand All @@ -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
}

Expand All @@ -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

Expand All @@ -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
}

Expand Down

0 comments on commit 8a8b40f

Please sign in to comment.