-
Notifications
You must be signed in to change notification settings - Fork 454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(gcp): enable organization validation #2133
Open
ericnorris
wants to merge
1
commit into
smallstep:master
Choose a base branch
from
ericnorris:feat-gcp-enable-organization-checking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−18
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package gcp | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/smallstep/certificates/errs" | ||
|
||
"google.golang.org/api/cloudresourcemanager/v1" | ||
) | ||
|
||
type ProjectValidator struct { | ||
ProjectIDs []string | ||
} | ||
|
||
func (p *ProjectValidator) ValidateProject(_ context.Context, projectID string) error { | ||
if len(p.ProjectIDs) == 0 { | ||
return nil | ||
} | ||
|
||
for _, pi := range p.ProjectIDs { | ||
if pi == projectID { | ||
return nil | ||
} | ||
} | ||
|
||
return errs.Unauthorized("gcp.authorizeToken; invalid gcp token - invalid project id") | ||
} | ||
|
||
type OrganizationValidator struct { | ||
*ProjectValidator | ||
|
||
OrganizationID string | ||
projectsService *cloudresourcemanager.ProjectsService | ||
} | ||
|
||
func NewOrganizationValidator(ctx context.Context, projectIDs []string, organizationID string) (*OrganizationValidator, error) { | ||
crm, err := cloudresourcemanager.NewService(ctx) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &OrganizationValidator{ | ||
&ProjectValidator{projectIDs}, | ||
organizationID, | ||
crm.Projects, | ||
}, nil | ||
} | ||
|
||
func (p *OrganizationValidator) ValidateProject(ctx context.Context, projectID string) error { | ||
err := p.ProjectValidator.ValidateProject(ctx, projectID) | ||
|
||
if p.OrganizationID == "" { | ||
return err | ||
} | ||
|
||
ancestry, err := p.projectsService. | ||
GetAncestry(projectID, &cloudresourcemanager.GetAncestryRequest{}). | ||
Context(ctx). | ||
Do() | ||
|
||
if err != nil { | ||
return errs.Wrap(http.StatusInternalServerError, err, "gcp.authorizeToken") | ||
} | ||
|
||
if len(ancestry.Ancestor) < 1 { | ||
return errs.InternalServer("gcp.authorizeToken; getAncestry response malformed") | ||
} | ||
|
||
progenitor := ancestry.Ancestor[len(ancestry.Ancestor)-1] | ||
|
||
if progenitor.ResourceId.Type != "organization" || progenitor.ResourceId.Id != p.OrganizationID { | ||
return errs.Unauthorized("gcp.authorizeToken; invalid gcp token - project does not belong to organization") | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package gcp | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestProjectValidator(t *testing.T) { | ||
validator := &ProjectValidator{ProjectIDs: []string{"allowed-1", "allowed-2"}} | ||
|
||
if err := validator.ValidateProject(context.Background(), "not-allowed"); err == nil { | ||
t.Errorf("ProjectValidator.ValidateProject() = nil, want err") | ||
} | ||
|
||
if err := validator.ValidateProject(context.Background(), "allowed-2"); err != nil { | ||
t.Errorf("ProjectValidator.ValidateProject() = %v, want nil", err) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
|
||
"github.com/smallstep/assert" | ||
"github.com/smallstep/certificates/api/render" | ||
"github.com/smallstep/certificates/authority/provisioner/gcp" | ||
) | ||
|
||
func TestGCP_Getters(t *testing.T) { | ||
|
@@ -292,7 +293,7 @@ func TestGCP_authorizeToken(t *testing.T) { | |
"fail/invalid-projectID": func(t *testing.T) test { | ||
p, err := generateGCP() | ||
assert.FatalError(t, err) | ||
p.ProjectIDs = []string{"foo", "bar"} | ||
p.projectValidator = &gcp.ProjectValidator{ProjectIDs: []string{"foo", "bar"}} | ||
tok, err := generateGCPToken(p.ServiceAccounts[0], | ||
"https://accounts.google.com", p.GetID(), | ||
"instance-id", "instance-name", "project-id", "zone", | ||
|
@@ -398,7 +399,7 @@ func TestGCP_authorizeToken(t *testing.T) { | |
for name, tt := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
tc := tt(t) | ||
if claims, err := tc.p.authorizeToken(tc.token); err != nil { | ||
if claims, err := tc.p.authorizeToken(context.Background(), tc.token); err != nil { | ||
if assert.NotNil(t, tc.err) { | ||
var sc render.StatusCodedError | ||
assert.Fatal(t, errors.As(err, &sc), "error does not implement StatusCodedError interface") | ||
|
@@ -430,7 +431,7 @@ func TestGCP_AuthorizeSign(t *testing.T) { | |
|
||
p3, err := generateGCP() | ||
assert.FatalError(t, err) | ||
p3.ProjectIDs = []string{"other-project-id"} | ||
p3.projectValidator = &gcp.ProjectValidator{ProjectIDs: []string{"other-project-id"}} | ||
p3.ServiceAccounts = []string{"[email protected]"} | ||
p3.InstanceAge = Duration{1 * time.Minute} | ||
|
||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the project validation only enforced when the organizationID is not set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question - because I structured these as complementary, not mutually exclusive. I'm open to changing that, but for now it's implemented as "if you're not in the project list but an organization ID is set and you're in the organization, you're okay". I believe this would allow an incremental adoption for users already using the project ID list feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes more sense to return an error even if the organization id is set.
I don't know if they are mutually exclusive, but if the project id is in the token and it doesn't match the ones in my configuration, I would expect an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make that change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can make that change, but let me explain - if you set the
projectIDs
andorganizationID
, would you not be confused that it was rejected when a token was in fact inside the organization?That's what I meant by them being mutually exclusive: if it worked the way you described, you could set either
projectIDs
ororganizationID
, but not both. I was thinking about how if someone had setprojectIDs
today, and wanted to try out theorganizationID
setting, rather than completely disablingprojectIDs
and enablingorganizationID
, they could enableorganizationID
, try it out with a few projects, and then removeprojectIDs
.Or maybe they want to allow their organization and one or a few projects from a separate organization? This would also require them to be complementary.
Again though, this is not something I personally feel strongly about, but my hunch is that this behavior is more user-friendly.