This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #203 from secrethub/release/v0.30.0
Release v0.30.0
- Loading branch information
Showing
16 changed files
with
600 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"time" | ||
) | ||
|
||
var ( | ||
ErrInvalidIDPLinkType = errAPI.Code("invalid_idp_link_type").StatusError("invalid IDP link type", http.StatusBadRequest) | ||
ErrInvalidGCPProjectID = errAPI.Code("invalid_gcp_project_id").StatusErrorPref("invalid GCP project ID: %s", http.StatusBadRequest) | ||
ErrVerifyingGCPAccessProof = errAPI.Code("gcp_verification_error").StatusError("could not verify GCP authorization", http.StatusInternalServerError) | ||
ErrInvalidGCPAuthorizationCode = errAPI.Code("invalid_authorization_code").StatusError("authorization code was not accepted by GCP", http.StatusPreconditionFailed) | ||
ErrGCPLinkPermissionDenied = errAPI.Code("gcp_permission_denied").StatusError("missing required projects.get permission to create link to GCP project", http.StatusPreconditionFailed) | ||
|
||
gcpProjectIDPattern = regexp.MustCompile("^[a-z][a-z0-9-]*[a-z0-9]$") | ||
) | ||
|
||
type CreateIdentityProviderLinkGCPRequest struct { | ||
RedirectURL string `json:"redirect_url"` | ||
AuthorizationCode string `json:"authorization_code"` | ||
} | ||
|
||
type IdentityProviderLinkType string | ||
|
||
const ( | ||
IdentityProviderLinkGCP IdentityProviderLinkType = "gcp" | ||
) | ||
|
||
// IdentityProviderLink is a prerequisite for creating some identity provider backed service accounts. | ||
// These links prove that a namespace's member has access to a resource (identified by the LinkedID) within | ||
// the identity provider. Once a link between a namespace and an identity provider has been created, from then on | ||
// service accounts can be created within the scope described by the LinkedID. For example, after creating a link | ||
// to a GCP Project, GCP service accounts within that project can be used for the GCP Identity Provider. | ||
// | ||
// The meaning of LinkedID depends on the type of the IdentityProviderLink in the following way: | ||
// - GCP: LinkedID is a GCP Project ID. | ||
type IdentityProviderLink struct { | ||
Type IdentityProviderLinkType `json:"type"` | ||
Namespace string `json:"namespace"` | ||
LinkedID string `json:"linked_id"` | ||
CreatedAt time.Time `json:"created_at"` | ||
} | ||
|
||
type OAuthConfig struct { | ||
ClientID string `json:"client_id"` | ||
AuthURI string `json:"auth_uri"` | ||
Scopes []string `json:"scopes"` | ||
ResultURL *url.URL `json:"result_url"` | ||
} | ||
|
||
// ValidateLinkedID calls the validation function corresponding to the link type and returns the corresponding result. | ||
func ValidateLinkedID(linkType IdentityProviderLinkType, linkedID string) error { | ||
switch linkType { | ||
case IdentityProviderLinkGCP: | ||
return ValidateGCPProjectID(linkedID) | ||
default: | ||
return ErrInvalidIDPLinkType | ||
} | ||
} | ||
|
||
// ValidateGCPProjectID returns an error if the provided value is not a valid GCP project ID. | ||
func ValidateGCPProjectID(projectID string) error { | ||
if len(projectID) < 6 || len(projectID) > 30 { | ||
return ErrInvalidGCPProjectID("length must be 6 to 30 character") | ||
} | ||
if !gcpProjectIDPattern.MatchString(projectID) { | ||
return ErrInvalidGCPProjectID("can only contains lowercase letter, digits and hyphens") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -474,13 +474,16 @@ func TestValidateGCPServiceAccountEmail(t *testing.T) { | |
in: "test-service-account@secrethub-test-1234567890.iam.gserviceaccount.com", | ||
}, | ||
"appspot service account": { | ||
in: "[email protected]", | ||
in: "[email protected]", | ||
expectErr: true, | ||
}, | ||
"compute service account": { | ||
in: "[email protected]", | ||
in: "[email protected]", | ||
expectErr: true, | ||
}, | ||
"google managed service account": { | ||
in: "[email protected]", | ||
in: "[email protected]", | ||
expectErr: true, | ||
}, | ||
"not an email": { | ||
in: "cloudservices.gserviceaccount.com", | ||
|
@@ -498,9 +501,37 @@ func TestValidateGCPServiceAccountEmail(t *testing.T) { | |
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
err := api.ValidateGCPServiceAccountEmail(tc.in) | ||
err := api.ValidateGCPUserManagedServiceAccountEmail(tc.in) | ||
|
||
assert.Equal(t, err != nil, tc.expectErr) | ||
}) | ||
} | ||
} | ||
|
||
func TestProjectIDFromGCPEmail(t *testing.T) { | ||
cases := map[string]struct { | ||
in string | ||
expectErr bool | ||
expect string | ||
}{ | ||
"user managed service account": { | ||
in: "test-service-account@secrethub-test-1234567890.iam.gserviceaccount.com", | ||
expect: "secrethub-test-1234567890", | ||
}, | ||
"invalid email": { | ||
in: "[email protected]", | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
projectID, err := api.ProjectIDFromGCPEmail(tc.in) | ||
|
||
assert.Equal(t, err != nil, tc.expectErr) | ||
if !tc.expectErr { | ||
assert.Equal(t, projectID, tc.expect) | ||
} | ||
}) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.