SaveCredentials saves credentials. If opts includes WithExistingSecret, upserts at the given path.
(ctx context.Context, orgID string, creds any, opts ...credentials.SaveOption)
| 83 | |
| 84 | // SaveCredentials saves credentials. If opts includes WithExistingSecret, upserts at the given path. |
| 85 | func (m *Manager) SaveCredentials(ctx context.Context, orgID string, creds any, opts ...credentials.SaveOption) (string, error) { |
| 86 | o := credentials.ApplySaveOptions(opts...) |
| 87 | secretName := o.SecretName |
| 88 | if secretName == "" { |
| 89 | secretName = strings.Join([]string{m.secretPrefix, orgID, uuid.New().String()}, "/") |
| 90 | } |
| 91 | |
| 92 | // Store the credentials as json key pairs |
| 93 | c, err := json.Marshal(creds) |
| 94 | if err != nil { |
| 95 | return "", fmt.Errorf("marshaling credentials to be stored: %w", err) |
| 96 | } |
| 97 | |
| 98 | if o.SecretName != "" { |
| 99 | // Upsert: try to update existing secret first |
| 100 | _, err = m.client.PutSecretValue(ctx, &secretsmanager.PutSecretValueInput{ |
| 101 | SecretId: aws.String(secretName), |
| 102 | SecretString: aws.String(string(c)), |
| 103 | }) |
| 104 | var notFoundErr *smtypes.ResourceNotFoundException |
| 105 | if errors.As(err, ¬FoundErr) { |
| 106 | // Secret does not exist yet, create it |
| 107 | _, err = m.client.CreateSecret(ctx, &secretsmanager.CreateSecretInput{ |
| 108 | Name: aws.String(secretName), |
| 109 | SecretString: aws.String(string(c)), |
| 110 | }) |
| 111 | } |
| 112 | } else { |
| 113 | _, err = m.client.CreateSecret(ctx, &secretsmanager.CreateSecretInput{ |
| 114 | Name: aws.String(secretName), |
| 115 | SecretString: aws.String(string(c)), |
| 116 | }) |
| 117 | } |
| 118 | |
| 119 | if err != nil { |
| 120 | return "", fmt.Errorf("saving secret in AWS: %w", err) |
| 121 | } |
| 122 | |
| 123 | return secretName, nil |
| 124 | } |
| 125 | |
| 126 | func (m *Manager) ReadCredentials(ctx context.Context, secretID string, creds any) error { |
| 127 | resp, err := m.client.GetSecretValue(ctx, &secretsmanager.GetSecretValueInput{ |
nothing calls this directly
no test coverage detected