Skip to content

Commit 9c27cdb

Browse files
heiytorotavio
authored andcommitted
refactor(api): change PublicKeyCreate to return fingerprint
- Change PublicKeyCreate return type from error to (string, error)
1 parent 10cd454 commit 9c27cdb

File tree

6 files changed

+35
-21
lines changed

6 files changed

+35
-21
lines changed

api/services/sshkeys.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ func (s *service) CreatePublicKey(ctx context.Context, req requests.PublicKeyCre
141141
},
142142
}
143143

144-
err = s.store.PublicKeyCreate(ctx, &model)
145-
if err != nil {
144+
if _, err := s.store.PublicKeyCreate(ctx, &model); err != nil {
146145
return nil, err
147146
}
148147

api/services/sshkeys_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ func TestCreatePublicKeys(t *testing.T) {
855855
}
856856

857857
storeMock.On("PublicKeyGet", ctx, keyWithHostname.Fingerprint, "tenant").Return(nil, store.ErrNoDocuments).Once()
858-
storeMock.On("PublicKeyCreate", ctx, &keyWithHostnameModel).Return(errors.New("error", "", 0)).Once()
858+
storeMock.On("PublicKeyCreate", ctx, &keyWithHostnameModel).Return("", errors.New("error", "", 0)).Once()
859859
},
860860
expected: Expected{nil, errors.New("error", "", 0)},
861861
},
@@ -894,7 +894,7 @@ func TestCreatePublicKeys(t *testing.T) {
894894
}
895895

896896
storeMock.On("PublicKeyGet", ctx, keyWithHostname.Fingerprint, "tenant").Return(nil, store.ErrNoDocuments).Once()
897-
storeMock.On("PublicKeyCreate", ctx, &keyWithHostnameModel).Return(nil).Once()
897+
storeMock.On("PublicKeyCreate", ctx, &keyWithHostnameModel).Return(ssh.FingerprintLegacyMD5(pubKey), nil).Once()
898898
},
899899
expected: Expected{&responses.PublicKeyCreate{
900900
Data: ssh.MarshalAuthorizedKey(pubKey),
@@ -952,7 +952,7 @@ func TestCreatePublicKeys(t *testing.T) {
952952
Once()
953953
storeMock.On("TagList", ctx, mock.AnythingOfType("store.QueryOption")).Return(tags, len(tags), nil).Once()
954954
storeMock.On("PublicKeyGet", ctx, keyWithTags.Fingerprint, "tenant").Return(nil, store.ErrNoDocuments).Once()
955-
storeMock.On("PublicKeyCreate", ctx, &keyWithTagsModel).Return(errors.New("error", "", 0)).Once()
955+
storeMock.On("PublicKeyCreate", ctx, &keyWithTagsModel).Return("", errors.New("error", "", 0)).Once()
956956
},
957957
expected: Expected{nil, errors.New("error", "", 0)},
958958
},
@@ -1000,7 +1000,7 @@ func TestCreatePublicKeys(t *testing.T) {
10001000
Once()
10011001
storeMock.On("TagList", ctx, mock.AnythingOfType("store.QueryOption")).Return(tags, len(tags), nil).Once()
10021002
storeMock.On("PublicKeyGet", ctx, keyWithTags.Fingerprint, "tenant").Return(nil, store.ErrNoDocuments).Once()
1003-
storeMock.On("PublicKeyCreate", ctx, &keyWithTagsModel).Return(nil).Once()
1003+
storeMock.On("PublicKeyCreate", ctx, &keyWithTagsModel).Return(ssh.FingerprintLegacyMD5(pubKey), nil).Once()
10041004
},
10051005
expected: Expected{&responses.PublicKeyCreate{
10061006
Data: ssh.MarshalAuthorizedKey(pubKey),

api/store/mocks/store.go

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/store/mongo/publickey.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ func (s *Store) PublicKeyList(ctx context.Context, opts ...store.QueryOption) ([
8888
return list, count, err
8989
}
9090

91-
func (s *Store) PublicKeyCreate(ctx context.Context, key *models.PublicKey) error {
91+
func (s *Store) PublicKeyCreate(ctx context.Context, key *models.PublicKey) (string, error) {
9292
bsonBytes, err := bson.Marshal(key)
9393
if err != nil {
94-
return FromMongoError(err)
94+
return "", FromMongoError(err)
9595
}
9696

9797
doc := make(bson.M)
9898
if err := bson.Unmarshal(bsonBytes, &doc); err != nil {
99-
return FromMongoError(err)
99+
return "", FromMongoError(err)
100100
}
101101

102102
// WORKAROUND: Convert string TagIDs to MongoDB ObjectIDs for referential integrity
@@ -110,10 +110,10 @@ func (s *Store) PublicKeyCreate(ctx context.Context, key *models.PublicKey) erro
110110
}
111111

112112
if _, err := s.db.Collection("public_keys").InsertOne(ctx, doc); err != nil {
113-
return FromMongoError(err)
113+
return "", FromMongoError(err)
114114
}
115115

116-
return nil
116+
return doc["fingerprint"].(string), nil
117117
}
118118

119119
func (s *Store) PublicKeyUpdate(ctx context.Context, publicKey *models.PublicKey) error {

api/store/mongo/publickey_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,16 @@ func TestPublicKeyList(t *testing.T) {
184184
}
185185

186186
func TestPublicKeyCreate(t *testing.T) {
187+
type Expected struct {
188+
fingerprint string
189+
err error
190+
}
191+
187192
cases := []struct {
188193
description string
189194
key *models.PublicKey
190195
fixtures []string
191-
expected error
196+
expected Expected
192197
}{
193198
{
194199
description: "succeeds when data is valid",
@@ -199,7 +204,7 @@ func TestPublicKeyCreate(t *testing.T) {
199204
PublicKeyFields: models.PublicKeyFields{Name: "public_key", Filter: models.PublicKeyFilter{Hostname: ".*"}},
200205
},
201206
fixtures: []string{},
202-
expected: nil,
207+
expected: Expected{fingerprint: "fingerprint", err: nil},
203208
},
204209
}
205210

@@ -212,8 +217,8 @@ func TestPublicKeyCreate(t *testing.T) {
212217
assert.NoError(t, srv.Reset())
213218
})
214219

215-
err := s.PublicKeyCreate(ctx, tc.key)
216-
assert.Equal(t, tc.expected, err)
220+
fingerprint, err := s.PublicKeyCreate(ctx, tc.key)
221+
assert.Equal(t, tc.expected, Expected{fingerprint: fingerprint, err: err})
217222
})
218223
}
219224
}

api/store/publickey.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
type PublicKeyStore interface {
1010
PublicKeyList(ctx context.Context, opts ...QueryOption) ([]models.PublicKey, int, error)
1111
PublicKeyGet(ctx context.Context, fingerprint string, tenantID string) (*models.PublicKey, error)
12-
PublicKeyCreate(ctx context.Context, key *models.PublicKey) error
12+
PublicKeyCreate(ctx context.Context, key *models.PublicKey) (string, error)
1313
PublicKeyUpdate(ctx context.Context, publicKey *models.PublicKey) error
1414
PublicKeyDelete(ctx context.Context, publicKey *models.PublicKey) error
1515
}

0 commit comments

Comments
 (0)