Skip to content

Commit 032fcc6

Browse files
committed
refactor: simplify auth secret handling
1 parent f2edb79 commit 032fcc6

File tree

6 files changed

+32
-38
lines changed

6 files changed

+32
-38
lines changed

api/gitinfo/auth.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,27 @@ func (a Auth) HasBasicAuth() bool {
3030
return a.Username != nil && a.Password != nil
3131
}
3232

33-
func (a Auth) Secret(app *apps.Application) *corev1.Secret {
34-
data := map[string][]byte{}
35-
36-
if a.SSHPrivateKey != nil {
37-
data[PrivateKeySecretKey] = []byte(*a.SSHPrivateKey)
38-
} else if a.Username != nil && a.Password != nil {
39-
data[UsernameSecretKey] = []byte(*a.Username)
40-
data[PasswordSecretKey] = []byte(*a.Password)
41-
}
42-
33+
// NewAuthSecret returns a new secret for the given application. It can be used as
34+
// a key for Get/Delete operations or as a base for populating credentials.
35+
func NewAuthSecret(app *apps.Application) *corev1.Secret {
4336
return &corev1.Secret{
4437
ObjectMeta: metav1.ObjectMeta{
4538
Name: AuthSecretName(app),
4639
Namespace: app.Namespace,
4740
},
48-
Data: data,
4941
}
5042
}
5143

52-
// UpdateSecret replaces the data of the secret with the data from GitAuth. Only
53-
// replaces fields which are non-nil.
54-
func (a Auth) UpdateSecret(secret *corev1.Secret) {
44+
// ApplyToSecret writes the Auth credentials into the given secret's Data field.
45+
// Only writes fields which are non-nil.
46+
func (a Auth) ApplyToSecret(secret *corev1.Secret) {
47+
if secret.Data == nil {
48+
secret.Data = make(map[string][]byte)
49+
}
50+
if secret.Annotations == nil {
51+
secret.Annotations = make(map[string]string)
52+
}
53+
5554
if a.SSHPrivateKey != nil {
5655
secret.Data[PrivateKeySecretKey] = []byte(*a.SSHPrivateKey)
5756
}
@@ -63,9 +62,6 @@ func (a Auth) UpdateSecret(secret *corev1.Secret) {
6362
if a.Password != nil {
6463
secret.Data[PasswordSecretKey] = []byte(*a.Password)
6564
}
66-
if secret.Annotations == nil {
67-
secret.Annotations = make(map[string]string)
68-
}
6965
}
7066

7167
// Enabled returns true if any kind of credentials are set in the GitAuth

create/application.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ func (cmd *applicationCmd) Run(ctx context.Context, client *api.Client) error {
161161
return fmt.Errorf("the credentials are given but they are empty: %w", err)
162162
}
163163

164-
secret := auth.Secret(newApp)
164+
secret := gitinfo.NewAuthSecret(newApp)
165+
auth.ApplyToSecret(secret)
165166
// for git auth we create a separate secret and then reference it in the app.
166167
if err := client.Create(ctx, secret); err != nil {
167168
if kerrors.IsAlreadyExists(err) {
@@ -172,7 +173,7 @@ func (cmd *applicationCmd) Run(ctx context.Context, client *api.Client) error {
172173
return err
173174
}
174175

175-
auth.UpdateSecret(secret)
176+
auth.ApplyToSecret(secret)
176177
if err := client.Update(ctx, secret); err != nil {
177178
return err
178179
}
@@ -201,7 +202,7 @@ func (cmd *applicationCmd) Run(ctx context.Context, client *api.Client) error {
201202

202203
if err := c.createResource(appWaitCtx); err != nil {
203204
if auth.Enabled() {
204-
secret := auth.Secret(newApp)
205+
secret := gitinfo.NewAuthSecret(newApp)
205206
if gitErr := client.Delete(ctx, secret); gitErr != nil {
206207
return errors.Join(err, fmt.Errorf("unable to delete git auth secret: %w", gitErr))
207208
}

create/application_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ func TestCreateApplication(t *testing.T) {
150150
},
151151
checkApp: func(t *testing.T, cmd applicationCmd, app *apps.Application) {
152152
is := require.New(t)
153-
auth := gitinfo.Auth{Username: cmd.Git.Username, Password: cmd.Git.Password}
154-
authSecret := auth.Secret(app)
153+
authSecret := gitinfo.NewAuthSecret(app)
155154
if err := apiClient.Get(t.Context(), api.ObjectName(authSecret), authSecret); err != nil {
156155
t.Fatal(err)
157156
}
@@ -176,8 +175,7 @@ func TestCreateApplication(t *testing.T) {
176175
},
177176
checkApp: func(t *testing.T, cmd applicationCmd, app *apps.Application) {
178177
is := require.New(t)
179-
auth := gitinfo.Auth{SSHPrivateKey: cmd.Git.SSHPrivateKey}
180-
authSecret := auth.Secret(app)
178+
authSecret := gitinfo.NewAuthSecret(app)
181179
if err := apiClient.Get(t.Context(), api.ObjectName(authSecret), authSecret); err != nil {
182180
t.Fatal(err)
183181
}
@@ -201,8 +199,7 @@ func TestCreateApplication(t *testing.T) {
201199
},
202200
checkApp: func(t *testing.T, cmd applicationCmd, app *apps.Application) {
203201
is := require.New(t)
204-
auth := gitinfo.Auth{SSHPrivateKey: cmd.Git.SSHPrivateKey}
205-
authSecret := auth.Secret(app)
202+
authSecret := gitinfo.NewAuthSecret(app)
206203
if err := apiClient.Get(t.Context(), api.ObjectName(authSecret), authSecret); err != nil {
207204
t.Fatal(err)
208205
}
@@ -226,8 +223,7 @@ func TestCreateApplication(t *testing.T) {
226223
},
227224
checkApp: func(t *testing.T, cmd applicationCmd, app *apps.Application) {
228225
is := require.New(t)
229-
auth := gitinfo.Auth{SSHPrivateKey: ptr.To("notused")}
230-
authSecret := auth.Secret(app)
226+
authSecret := gitinfo.NewAuthSecret(app)
231227
if err := apiClient.Get(t.Context(), api.ObjectName(authSecret), authSecret); err != nil {
232228
t.Fatal(err)
233229
}
@@ -251,8 +247,7 @@ func TestCreateApplication(t *testing.T) {
251247
},
252248
checkApp: func(t *testing.T, cmd applicationCmd, app *apps.Application) {
253249
is := require.New(t)
254-
auth := gitinfo.Auth{SSHPrivateKey: ptr.To("notused")}
255-
authSecret := auth.Secret(app)
250+
authSecret := gitinfo.NewAuthSecret(app)
256251
if err := apiClient.Get(t.Context(), api.ObjectName(authSecret), authSecret); err != nil {
257252
t.Fatal(err)
258253
}

delete/application_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func dummyApp(name, namespace string) *apps.Application {
214214
}
215215

216216
func gitSecretFor(app *apps.Application) *corev1.Secret {
217-
s := gitinfo.Auth{}.Secret(app)
217+
s := gitinfo.NewAuthSecret(app)
218218
s.TypeMeta = metav1.TypeMeta{
219219
APIVersion: corev1.SchemeGroupVersion.String(),
220220
Kind: "Secret",

update/application.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ func (cmd *applicationCmd) Run(ctx context.Context, client *api.Client) error {
195195
}
196196

197197
if auth.Enabled() {
198-
secret := auth.Secret(app)
198+
secret := gitinfo.NewAuthSecret(app)
199199
if err := client.Get(ctx, client.Name(secret.Name), secret); err != nil {
200200
if errors.IsNotFound(err) {
201-
auth.UpdateSecret(secret)
201+
auth.ApplyToSecret(secret)
202202
if err := client.Create(ctx, secret); err != nil {
203203
return err
204204
}
@@ -209,7 +209,7 @@ func (cmd *applicationCmd) Run(ctx context.Context, client *api.Client) error {
209209
return err
210210
}
211211

212-
auth.UpdateSecret(secret)
212+
auth.ApplyToSecret(secret)
213213
if err := client.Update(ctx, secret); err != nil {
214214
return err
215215
}
@@ -501,12 +501,12 @@ func warnIfDockerfileNotEnabled(w format.Writer, app *apps.Application, flag str
501501
}
502502

503503
func gitAuthFromApp(ctx context.Context, client *api.Client, app *apps.Application) (gitinfo.Auth, error) {
504-
auth := &gitinfo.Auth{}
505-
secret := auth.Secret(app)
504+
secret := gitinfo.NewAuthSecret(app)
506505
if err := client.Get(ctx, client.Name(secret.Name), secret); err != nil {
507506
return gitinfo.Auth{}, err
508507
}
508+
var auth gitinfo.Auth
509509
auth.UpdateFromSecret(secret)
510510

511-
return *auth, nil
511+
return auth, nil
512512
}

update/application_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,9 @@ func TestApplication(t *testing.T) {
620620

621621
objects := []client.Object{tc.orig}
622622
if tc.gitAuth != nil {
623-
objects = append(objects, tc.gitAuth.Secret(tc.orig))
623+
secret := gitinfo.NewAuthSecret(tc.orig)
624+
tc.gitAuth.ApplyToSecret(secret)
625+
objects = append(objects, secret)
624626
}
625627
apiClient := test.SetupClient(t,
626628
test.WithObjects(objects...),

0 commit comments

Comments
 (0)