Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 50206dd

Browse files
Merge pull request #232 from secrethub/release/v0.32.0
Release v0.32.0
2 parents f9c0186 + e3b147d commit 50206dd

24 files changed

Lines changed: 475 additions & 576 deletions

internals/api/secret.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
ErrNoSecretMembers = errAPI.Code("no_secret_members").StatusError("no secret members added to write request", http.StatusBadRequest)
3232

3333
ErrInvalidSecretKeyID = errAPI.Code("invalid_secret_key_id").StatusError("secret_key_id is invalid", http.StatusBadRequest)
34-
ErrNotEncryptedForAccounts = errAPI.Code("not_encrypted_for_accounts").StatusError("missing data encrypted for accounts", http.StatusBadRequest)
34+
ErrNotEncryptedForAccounts = errAPI.Code("not_encrypted_for_accounts").StatusError("missing data encrypted for accounts. This can occur when access rules are simultaneously created with resources controlled by the access rule. You may try again.", http.StatusConflict)
3535
ErrNotUniquelyEncryptedForAccounts = errAPI.Code("not_uniquely_encrypted_for_accounts").StatusError("not uniquely encrypted for accounts", http.StatusBadRequest)
3636

3737
ErrCannotDeleteLastSecretVersion = errAPI.Code("cannot_delete_last_version").StatusError("Cannot delete the last version of a secret", http.StatusForbidden)

internals/api/service.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ func (a Service) ToAuditActor() *AuditActor {
6666
type CreateServiceRequest struct {
6767
Description string `json:"description"`
6868
Credential *CreateCredentialRequest `json:"credential"`
69-
AccountKey *CreateAccountKeyRequest `json:"account_key"`
7069
RepoMember *CreateRepoMemberRequest `json:"repo_member"`
7170
}
7271

@@ -83,10 +82,10 @@ func (req CreateServiceRequest) Validate() error {
8382
return err
8483
}
8584

86-
if req.AccountKey == nil {
85+
if req.Credential.AccountKey == nil {
8786
return ErrMissingField("account_key")
8887
}
89-
if err := req.AccountKey.Validate(); err != nil {
88+
if err := req.Credential.AccountKey.Validate(); err != nil {
9089
return err
9190
}
9291

internals/api/user.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -85,56 +85,3 @@ func (u User) ToAuditActor() *AuditActor {
8585
User: u.Trim(),
8686
}
8787
}
88-
89-
// CreateUserRequest contains the required fields for signing up
90-
type CreateUserRequest struct {
91-
Username string `json:"username"`
92-
Email string `json:"email"`
93-
FullName string `json:"full_name"`
94-
Password string `json:"password,omitempty"`
95-
Credential *CreateCredentialRequest `json:"credential,omitempty"`
96-
}
97-
98-
// Validate validates the request fields.
99-
func (req *CreateUserRequest) Validate() error {
100-
err := ValidateUsername(req.Username)
101-
if err != nil {
102-
return err
103-
}
104-
105-
if req.Credential == nil && req.Password == "" {
106-
return ErrNoPasswordNorCredential
107-
}
108-
109-
if req.Credential != nil {
110-
err = req.Credential.Validate()
111-
if err != nil {
112-
return err
113-
}
114-
}
115-
116-
err = ValidateEmail(req.Email)
117-
if err != nil {
118-
return err
119-
}
120-
121-
err = ValidateFullName(req.FullName)
122-
if err != nil {
123-
return err
124-
}
125-
return nil
126-
}
127-
128-
// CreateFederatedUserRequest contains the required fields for signing up with a federated user
129-
type CreateFederatedUserRequest struct {
130-
Username string `json:"username"`
131-
}
132-
133-
// Validate validates the request fields.
134-
func (req CreateFederatedUserRequest) Validate() error {
135-
err := ValidateUsername(req.Username)
136-
if err != nil {
137-
return err
138-
}
139-
return nil
140-
}

internals/api/user_test.go

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"strings"
66
"testing"
7-
8-
"github.com/secrethub/secrethub-go/internals/assert"
97
)
108

119
func TestValidateUsername(t *testing.T) {
@@ -168,78 +166,3 @@ func TestValidateFullName(t *testing.T) {
168166
}
169167
}
170168
}
171-
172-
func TestCreateUserRequest_Validate(t *testing.T) {
173-
cases := map[string]struct {
174-
req CreateUserRequest
175-
err error
176-
}{
177-
"valid using password": {
178-
req: CreateUserRequest{
179-
Username: "test.-_UserTestT",
180-
Email: "test-account.dev1@secrethub.io",
181-
FullName: "Test Tester",
182-
Password: "hello world",
183-
},
184-
err: nil,
185-
},
186-
"valid using credential": {
187-
req: CreateUserRequest{
188-
Username: "test.-_UserTestT",
189-
Email: "test-account.dev1@secrethub.io",
190-
FullName: "Test Tester",
191-
Credential: &CreateCredentialRequest{
192-
Type: CredentialTypeKey,
193-
Fingerprint: "88c9eae68eb300b2971a2bec9e5a26ff4179fd661d6b7d861e4c6557b9aaee14",
194-
Verifier: []byte("verifier"),
195-
},
196-
},
197-
err: nil,
198-
},
199-
"invalid no password nor credential": {
200-
req: CreateUserRequest{
201-
Username: "test.-_UserTestT",
202-
Email: "test-account.dev1@secrethub.io",
203-
FullName: "Test Tester",
204-
},
205-
err: ErrNoPasswordNorCredential,
206-
},
207-
"invalid username": {
208-
req: CreateUserRequest{
209-
Username: "",
210-
Email: "test-account.dev1@secrethub.io",
211-
FullName: "Test Tester",
212-
Password: "hello world",
213-
},
214-
err: ErrInvalidUsername,
215-
},
216-
"invalid email": {
217-
req: CreateUserRequest{
218-
Username: "test",
219-
Email: "notanemail",
220-
FullName: "Test Tester",
221-
Password: "hello world",
222-
},
223-
err: ErrInvalidEmail,
224-
},
225-
"invalid full name": {
226-
req: CreateUserRequest{
227-
Username: "test",
228-
Email: "test-account.dev1@secrethub.io",
229-
FullName: "",
230-
Password: "hello world",
231-
},
232-
err: ErrInvalidFullName,
233-
},
234-
}
235-
236-
for name, tc := range cases {
237-
t.Run(name, func(t *testing.T) {
238-
// Do
239-
err := tc.req.Validate()
240-
241-
// Assert
242-
assert.Equal(t, err, tc.err)
243-
})
244-
}
245-
}

internals/errio/errors.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func UnexpectedError(err error) PublicError {
158158
"an unexpected error occurred: %v\n\nTry again later or contact support@secrethub.io if the problem persists",
159159
err,
160160
),
161+
err: err,
161162
}
162163
}
163164

@@ -190,6 +191,7 @@ type PublicError struct {
190191
Namespace Namespace `json:"namespace,omitempty"`
191192
Code string `json:"code"`
192193
Message string `json:"message"`
194+
err error
193195
}
194196

195197
// PublicError implements the error interface.
@@ -221,6 +223,11 @@ func (e PublicError) Type() string {
221223
return fmt.Sprintf("%s.%s", e.Namespace, e.Code)
222224
}
223225

226+
// Unwrap returns the wrapped error if the PublicError represents an error wrapped as an UnexpectedError.
227+
func (e PublicError) Unwrap() error {
228+
return e.err
229+
}
230+
224231
// PublicStatusError represents an http error. It contains an HTTP status
225232
// code and can be json encoded in an HTTP response.
226233
type PublicStatusError struct {
@@ -263,3 +270,14 @@ func Equals(a PublicError, b error) bool {
263270
}
264271
return a.Namespace == publicError.Namespace && a.Code == publicError.Code
265272
}
273+
274+
// EqualsAPIError checks whether the given error has the namespace and code of the
275+
// given API error. The HTTP status code and error message aren't checked, so this
276+
// function is compatible with any changes to the message and HTTP status code.
277+
func EqualsAPIError(apiErr PublicStatusError, err error) bool {
278+
publicStatusError, ok := err.(PublicStatusError)
279+
if !ok {
280+
return false
281+
}
282+
return Equals(apiErr.PublicError, publicStatusError.PublicError)
283+
}

pkg/secrethub/account.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,23 @@ func (c *Client) createAccountKeyRequest(encrypter credentials.Encrypter, accoun
7878
}, nil
7979
}
8080

81-
func (c *Client) createCredentialRequest(verifier credentials.Verifier, metadata map[string]string) (*api.CreateCredentialRequest, error) {
81+
func (c *Client) createCredentialRequest(encrypter credentials.Encrypter, accountKey crypto.RSAPrivateKey, verifier credentials.Verifier, metadata map[string]string) (*api.CreateCredentialRequest, error) {
8282
bytes, fingerprint, err := verifier.Export()
8383
if err != nil {
8484
return nil, errio.Error(err)
8585
}
8686

87+
accountKeyReq, err := c.createAccountKeyRequest(encrypter, accountKey)
88+
if err != nil {
89+
return nil, err
90+
}
91+
8792
req := api.CreateCredentialRequest{
8893
Fingerprint: fingerprint,
8994
Verifier: bytes,
9095
Type: verifier.Type(),
9196
Metadata: metadata,
97+
AccountKey: accountKeyReq,
9298
}
9399
err = verifier.AddProof(&req)
94100
if err != nil {

0 commit comments

Comments
 (0)