MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / SaveCredentials

Method SaveCredentials

pkg/credentials/aws/secretmanager.go:85–124  ·  view source on GitHub ↗

SaveCredentials saves credentials. If opts includes WithExistingSecret, upserts at the given path.

(ctx context.Context, orgID string, creds any, opts ...credentials.SaveOption)

Source from the content-addressed store, hash-verified

83
84// SaveCredentials saves credentials. If opts includes WithExistingSecret, upserts at the given path.
85func (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, &notFoundErr) {
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
126func (m *Manager) ReadCredentials(ctx context.Context, secretID string, creds any) error {
127 resp, err := m.client.GetSecretValue(ctx, &secretsmanager.GetSecretValueInput{

Callers

nothing calls this directly

Calls 4

ApplySaveOptionsFunction · 0.92
StringMethod · 0.65
PutSecretValueMethod · 0.65
CreateSecretMethod · 0.65

Tested by

no test coverage detected