-
Notifications
You must be signed in to change notification settings - Fork 8
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
CSS-5674 Replace database-stored access with OpenFGA #1051
Merged
babakks
merged 28 commits into
canonical:feature-rebac
from
babakks:css-5674/replace-db-with-openfga
Oct 12, 2023
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
0a3479e
JIMM uses JWT to log in to individual controllers.
alesstimec b02d546
Update godocs to indicate idempotency
babakks 1f0b28b
Add `unsetMultipleResourceAccess`
babakks e0b6310
Replace database-stored relations with OpenFGA
babakks 72db748
Un-comment grant/revoke cloud access tests
babakks 6adda6d
Use existing `ToCloudRelation` for mapping accesses to relations
babakks a9fbc13
Merge branch 'feature-rebac' into css-5674/replace-db-with-openfga
babakks 1ce4d99
Add `UnsetModelAccess` method
babakks f3fde61
Assert for tuples that should exist after revoking cloud access
babakks 9517dc9
Update `ModifyModelAccess` to change state in OpenFGA
babakks fdcd5ed
Update `GrantModelAccess` tests
babakks a755c0e
Update `RevokeModelAccess` tests
babakks 46a4957
Improve revoke cloud access tests
babakks a090d49
Improve revoke model access tests
babakks 3e3b83e
Add more revoke cloud access cases having all relations separately
babakks 5e85687
Add more revoke model access cases having all relations separately
babakks b475081
Change if-statements with switch-statements
babakks 0a467c4
Remove unnecessary Juju API call to grant/revoke model access
babakks f435efe
Remove unnecessary Juju API call to grant/revoke cloud access
babakks 9a88556
Improve failure error message
babakks 03916b2
Change to inline if-err checks where possible
babakks 543f39d
Improve local var naming
babakks e1ade1a
Add `op` to re-thrown error
babakks e43cc66
Remove unnecessary test for idempotent re-granting of cloud access
babakks 0cf9f3e
Merge branch 'feature-rebac' into css-5674/replace-db-with-openfga
babakks 42a373b
Log errors when granting/revoking access
babakks 29ac788
Add tests to verify returning unrecognized access error
babakks 92b3b71
Improve naming
babakks 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
|
||
jujuparams "github.com/juju/juju/rpc/params" | ||
"github.com/juju/names/v4" | ||
"github.com/juju/zaputil" | ||
"github.com/juju/zaputil/zapctx" | ||
"go.uber.org/zap" | ||
|
||
|
@@ -552,124 +553,140 @@ func (j *JIMM) doCloudAdmin(ctx context.Context, u *openfga.User, ct names.Cloud | |
// given user. If the cloud is not found then an error with the code | ||
// CodeNotFound is returned. If the authenticated user does not have admin | ||
// access to the cloud then an error with the code CodeUnauthorized is | ||
// returned. If the ModifyCloudAccess API call retuns an error the error | ||
// code is not masked. | ||
func (j *JIMM) GrantCloudAccess(ctx context.Context, u *dbmodel.User, ct names.CloudTag, ut names.UserTag, access string) error { | ||
// returned. | ||
func (j *JIMM) GrantCloudAccess(ctx context.Context, user *openfga.User, ct names.CloudTag, ut names.UserTag, access string) error { | ||
const op = errors.Op("jimm.GrantCloudAccess") | ||
|
||
// TODO (alesstimec) granting and revoking access tbd in a followup | ||
return errors.E(errors.CodeNotImplemented) | ||
/* | ||
ale := dbmodel.AuditLogEntry{ | ||
Time: time.Now().UTC().Round(time.Millisecond), | ||
Tag: ct.String(), | ||
UserTag: u.Tag().String(), | ||
Action: "grant", | ||
Params: dbmodel.StringMap{ | ||
"user": ut.String(), | ||
"access": access, | ||
}, | ||
targetRelation, err := ToCloudRelation(access) | ||
if err != nil { | ||
zapctx.Debug( | ||
ctx, | ||
"failed to recognize given access", | ||
zaputil.Error(err), | ||
zap.String("access", string(access)), | ||
) | ||
return errors.E(op, errors.CodeBadRequest, fmt.Sprintf("failed to recognize given access: %q", access), err) | ||
} | ||
|
||
err = j.doCloudAdmin(ctx, user, ct, func(_ *dbmodel.Cloud, _ API) error { | ||
targetUser := &dbmodel.User{} | ||
targetUser.SetTag(ut) | ||
if err := j.Database.GetUser(ctx, targetUser); err != nil { | ||
return err | ||
} | ||
defer j.addAuditLogEntry(&ale) | ||
targetOfgaUser := openfga.NewUser(targetUser, j.OpenFGAClient) | ||
|
||
err := j.doCloudAdmin(ctx, u, ct, func(c *dbmodel.Cloud, api API) error { | ||
targetUser := dbmodel.User{ | ||
Username: ut.Id(), | ||
} | ||
if err := j.Database.GetUser(ctx, &targetUser); err != nil { | ||
return err | ||
} | ||
if err := api.GrantCloudAccess(ctx, ct, ut, access); err != nil { | ||
return err | ||
currentRelation := targetOfgaUser.GetCloudAccess(ctx, ct) | ||
switch targetRelation { | ||
case ofganames.CanAddModelRelation: | ||
switch currentRelation { | ||
case ofganames.NoRelation: | ||
break | ||
default: | ||
return nil | ||
} | ||
var uca dbmodel.UserCloudAccess | ||
for _, a := range c.Users { | ||
if a.Username == targetUser.Username { | ||
uca = a | ||
break | ||
} | ||
case ofganames.AdministratorRelation: | ||
switch currentRelation { | ||
case ofganames.NoRelation, ofganames.CanAddModelRelation: | ||
break | ||
default: | ||
return nil | ||
} | ||
uca.User = targetUser | ||
uca.Cloud = *c | ||
uca.Access = access | ||
} | ||
|
||
if err := j.Database.UpdateUserCloudAccess(ctx, &uca); err != nil { | ||
return errors.E(op, err, "cannot update database after updating controller") | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
ale.Params["err"] = err.Error() | ||
return errors.E(op, err) | ||
if err := targetOfgaUser.SetCloudAccess(ctx, ct, targetRelation); err != nil { | ||
return errors.E(err, op, "failed to set cloud access") | ||
} | ||
ale.Success = true | ||
return nil | ||
*/ | ||
}) | ||
|
||
if err != nil { | ||
zapctx.Error( | ||
ctx, | ||
"failed to grant cloud access", | ||
zaputil.Error(err), | ||
zap.String("targetUser", string(ut.Id())), | ||
zap.String("cloud", string(ct.Id())), | ||
zap.String("access", string(access)), | ||
) | ||
return errors.E(op, err) | ||
} | ||
return nil | ||
} | ||
|
||
// RevokeCloudAccess revokes the given access level on the given cloud from | ||
// the given user. If the cloud is not found then an error with the code | ||
// CodeNotFound is returned. If the authenticated user does not have admin | ||
// access to the cloud then an error with the code CodeUnauthorized is | ||
// returned. If the ModifyCloudAccess API call retuns an error the error | ||
// code is not masked. | ||
func (j *JIMM) RevokeCloudAccess(ctx context.Context, u *dbmodel.User, ct names.CloudTag, ut names.UserTag, access string) error { | ||
// returned. | ||
func (j *JIMM) RevokeCloudAccess(ctx context.Context, user *openfga.User, ct names.CloudTag, ut names.UserTag, access string) error { | ||
const op = errors.Op("jimm.RevokeCloudAccess") | ||
|
||
// TODO (alesstimec) granting and revoking access tbd in a followup | ||
return errors.E(errors.CodeNotImplemented) | ||
|
||
/* | ||
ale := dbmodel.AuditLogEntry{ | ||
Time: time.Now().UTC().Round(time.Millisecond), | ||
Tag: ct.String(), | ||
UserTag: u.Tag().String(), | ||
Action: "revoke", | ||
Params: dbmodel.StringMap{ | ||
"user": ut.String(), | ||
"access": access, | ||
}, | ||
targetRelation, err := ToCloudRelation(access) | ||
if err != nil { | ||
zapctx.Debug( | ||
ctx, | ||
"failed to recognize given access", | ||
zaputil.Error(err), | ||
zap.String("access", string(access)), | ||
) | ||
return errors.E(op, errors.CodeBadRequest, fmt.Sprintf("failed to recognize given access: %q", access), err) | ||
} | ||
|
||
err = j.doCloudAdmin(ctx, user, ct, func(_ *dbmodel.Cloud, _ API) error { | ||
ale8k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
targetUser := &dbmodel.User{} | ||
targetUser.SetTag(ut) | ||
if err := j.Database.GetUser(ctx, targetUser); err != nil { | ||
return err | ||
} | ||
defer j.addAuditLogEntry(&ale) | ||
targetOfgaUser := openfga.NewUser(targetUser, j.OpenFGAClient) | ||
|
||
err := j.doCloudAdmin(ctx, u, ct, func(c *dbmodel.Cloud, api API) error { | ||
targetUser := dbmodel.User{ | ||
Username: ut.Id(), | ||
} | ||
if err := j.Database.GetUser(ctx, &targetUser); err != nil { | ||
return err | ||
} | ||
if err := api.RevokeCloudAccess(ctx, ct, ut, access); err != nil { | ||
return err | ||
} | ||
var uca dbmodel.UserCloudAccess | ||
for _, a := range c.Users { | ||
if a.Username == targetUser.Username { | ||
uca = a | ||
break | ||
currentRelation := targetOfgaUser.GetCloudAccess(ctx, ct) | ||
|
||
var relationsToRevoke []openfga.Relation | ||
switch targetRelation { | ||
case ofganames.CanAddModelRelation: | ||
switch currentRelation { | ||
case ofganames.NoRelation: | ||
return nil | ||
default: | ||
// If we're revoking "add-model" access, in addition to the "add-model" relation, we should also revoke the | ||
// "admin" relation. That's because having an "admin" relation indirectly grants the "add-model" permission | ||
// to the user. | ||
relationsToRevoke = []openfga.Relation{ | ||
ofganames.CanAddModelRelation, | ||
ofganames.AdministratorRelation, | ||
} | ||
} | ||
uca.User = targetUser | ||
uca.Cloud = *c | ||
switch access { | ||
case "admin": | ||
uca.Access = "add-model" | ||
case ofganames.AdministratorRelation: | ||
switch currentRelation { | ||
case ofganames.NoRelation, ofganames.CanAddModelRelation: | ||
return nil | ||
default: | ||
uca.Access = "" | ||
relationsToRevoke = []openfga.Relation{ | ||
ofganames.AdministratorRelation, | ||
} | ||
} | ||
} | ||
|
||
if err := j.Database.UpdateUserCloudAccess(ctx, &uca); err != nil { | ||
return errors.E(op, err, "cannot update database after updating controller") | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
ale.Params["err"] = err.Error() | ||
return errors.E(op, err) | ||
if err := targetOfgaUser.UnsetCloudAccess(ctx, ct, relationsToRevoke...); err != nil { | ||
return errors.E(err, op, "failed to unset cloud access") | ||
} | ||
ale.Success = true | ||
return nil | ||
*/ | ||
}) | ||
|
||
if err != nil { | ||
zapctx.Error( | ||
ctx, | ||
"failed to revoke cloud access", | ||
zaputil.Error(err), | ||
zap.String("targetUser", string(ut.Id())), | ||
zap.String("cloud", string(ct.Id())), | ||
zap.String("access", string(access)), | ||
) | ||
return errors.E(op, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please log this error |
||
} | ||
return nil | ||
} | ||
|
||
// RemoveCloud removes the given cloud from JAAS If the cloud is not found | ||
|
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.
please log this error