-
Notifications
You must be signed in to change notification settings - Fork 256
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
Add support for the subuser calls in the rgw admin interface #644
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4552269
rgw/admin: Clean up the parameter serialization
sebastianriese ba13594
rgw/admin: Extend the testing of buildQueryPath
sebastianriese 6ce73f3
rgw/admin: Extend the User data to parse Subuser data
sebastianriese f8dcafb
rgw/admin: Add the subuser API calls
sebastianriese 425a77b
rgw/admin: Generate API documentation for the new methods
sebastianriese fac2574
rgw/admin: Tests for the subuser API calls
sebastianriese 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
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
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,107 @@ | ||
//go:build ceph_preview | ||
// +build ceph_preview | ||
|
||
package admin | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// validateSubuserAcess - Return whether the given subuser access value is valid as input parameter | ||
func (s SubuserSpec) validateSubuserAccess() bool { | ||
a := s.Access | ||
return a == "" || | ||
a == SubuserAccessRead || | ||
a == SubuserAccessWrite || | ||
a == SubuserAccessReadWrite || | ||
a == SubuserAccessFull | ||
} | ||
|
||
func makeInvalidSubuserAccessLevelError(spec SubuserSpec) error { | ||
return fmt.Errorf("invalid subuser access level %q", spec.Access) | ||
} | ||
|
||
// The following are the subuser API functions. | ||
// | ||
// We need to explain the omission of ?subuser in the API path common | ||
// to all three functions. | ||
// | ||
// According to the docs, this has to be included to select the | ||
// subuser operation, but we already have subuser as a parameter with | ||
// a value (and make sure it's not empty and thus included by | ||
// validating the SubuserSpec). The presence of this parameter | ||
// triggers the subuser operation. | ||
// | ||
// If we add the subuser with the empty value the API call fails as | ||
// having an invalid signature (and it is semantically wrong as we | ||
// then have *two* values for the subuser name, an empty one an the | ||
// relevant one, the upstream code does not seem to handle that case | ||
// gracefully). | ||
|
||
// CreateSubuser - https://docs.ceph.com/en/latest/radosgw/adminops/#create-subuser | ||
// PREVIEW | ||
func (api *API) CreateSubuser(ctx context.Context, user User, subuser SubuserSpec) error { | ||
if user.ID == "" { | ||
return errMissingUserID | ||
} | ||
if subuser.Name == "" { | ||
return errMissingSubuserID | ||
} | ||
if !subuser.validateSubuserAccess() { | ||
return makeInvalidSubuserAccessLevelError(subuser) | ||
} | ||
// valid parameters not supported by go-ceph: access-key, gen-access-key | ||
v := valueToURLParams(user, []string{"uid"}) | ||
addToURLParams(&v, subuser, []string{"subuser", "access", "secret-key", "generate-secret", "key-type"}) | ||
_, err := api.call(ctx, http.MethodPut, "/user", v) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// RemoveSubuser - https://docs.ceph.com/en/latest/radosgw/adminops/#remove-subuser | ||
// PREVIEW | ||
func (api *API) RemoveSubuser(ctx context.Context, user User, subuser SubuserSpec) error { | ||
if user.ID == "" { | ||
return errMissingUserID | ||
} | ||
if subuser.Name == "" { | ||
return errMissingSubuserID | ||
} | ||
|
||
v := valueToURLParams(user, []string{"uid"}) | ||
addToURLParams(&v, subuser, []string{"subuser", "purge-keys"}) | ||
_, err := api.call(ctx, http.MethodDelete, "/user", v) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ModifySubuser - https://docs.ceph.com/en/latest/radosgw/adminops/#modify-subuser | ||
// PREVIEW | ||
func (api *API) ModifySubuser(ctx context.Context, user User, subuser SubuserSpec) error { | ||
if user.ID == "" { | ||
return errMissingUserID | ||
} | ||
if subuser.Name == "" { | ||
return errMissingSubuserID | ||
} | ||
if !subuser.validateSubuserAccess() { | ||
return makeInvalidSubuserAccessLevelError(subuser) | ||
} | ||
|
||
v := valueToURLParams(user, []string{"uid"}) | ||
addToURLParams(&v, subuser, []string{"subuser", "access", "secret", "generate-secret", "key-type"}) | ||
_, err := api.call(ctx, http.MethodPost, "/user", v) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
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,72 @@ | ||
package admin | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateSubuserAccess(t *testing.T) { | ||
assert.True(t, SubuserSpec{Access: SubuserAccessNone}.validateSubuserAccess()) | ||
assert.True(t, SubuserSpec{Access: SubuserAccessRead}.validateSubuserAccess()) | ||
assert.True(t, SubuserSpec{Access: SubuserAccessWrite}.validateSubuserAccess()) | ||
assert.True(t, SubuserSpec{Access: SubuserAccessReadWrite}.validateSubuserAccess()) | ||
assert.True(t, SubuserSpec{Access: SubuserAccessFull}.validateSubuserAccess()) | ||
assert.False(t, SubuserSpec{Access: SubuserAccessReplyFull}.validateSubuserAccess()) | ||
assert.False(t, SubuserSpec{Access: SubuserAccess("bar")}.validateSubuserAccess()) | ||
} | ||
|
||
func TestCreateSubuserMockAPI(t *testing.T) { | ||
t.Run("test create subuser validation: neither is set", func(t *testing.T) { | ||
api, err := New("127.0.0.1", "accessKey", "secretKey", returnMockClientCreateSubuser()) | ||
assert.NoError(t, err) | ||
err = api.CreateSubuser(context.TODO(), User{}, SubuserSpec{}) | ||
assert.Equal(t, err, errMissingUserID) | ||
}) | ||
t.Run("test create subuser validation: no user ID", func(t *testing.T) { | ||
api, err := New("127.0.0.1", "accessKey", "secretKey", returnMockClientCreateSubuser()) | ||
assert.NoError(t, err) | ||
err = api.CreateSubuser(context.TODO(), User{}, SubuserSpec{Name: "foo"}) | ||
assert.Equal(t, err, errMissingUserID) | ||
}) | ||
t.Run("test create subuser validation: no subuser ID", func(t *testing.T) { | ||
api, err := New("127.0.0.1", "accessKey", "secretKey", returnMockClientCreateSubuser()) | ||
assert.NoError(t, err) | ||
err = api.CreateSubuser(context.TODO(), User{ID: "dashboard-admin"}, SubuserSpec{}) | ||
assert.Equal(t, err, errMissingSubuserID) | ||
}) | ||
t.Run("test create subuser validation: valid", func(t *testing.T) { | ||
api, err := New("127.0.0.1", "accessKey", "secretKey", returnMockClientCreateSubuser()) | ||
assert.NoError(t, err) | ||
err = api.CreateSubuser(context.TODO(), User{ID: "dashboard-admin"}, SubuserSpec{Name: "foo"}) | ||
assert.NoError(t, err) | ||
}) | ||
t.Run("test create subuser validation: invalid access", func(t *testing.T) { | ||
api, err := New("127.0.0.1", "accessKey", "secretKey", returnMockClientCreateSubuser()) | ||
assert.NoError(t, err) | ||
err = api.CreateSubuser(context.TODO(), User{ID: "dashboard-admin"}, SubuserSpec{Name: "foo", Access: SubuserAccess("foo")}) | ||
assert.Error(t, err) | ||
assert.EqualError(t, err, `invalid subuser access level "foo"`) | ||
}) | ||
} | ||
|
||
// mockClient is defined in user_test.go | ||
func returnMockClientCreateSubuser() *mockClient { | ||
r := ioutil.NopCloser(bytes.NewReader([]byte(""))) | ||
return &mockClient{ | ||
mockDo: func(req *http.Request) (*http.Response, error) { | ||
if req.Method == http.MethodPut && req.URL.Path == "127.0.0.1/admin/user" { | ||
return &http.Response{ | ||
StatusCode: 201, | ||
Body: r, | ||
}, nil | ||
} | ||
return nil, fmt.Errorf("unexpected request: %q. method %q. path %q", req.URL.RawQuery, req.Method, req.URL.Path) | ||
}, | ||
} | ||
} |
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.
Is this purging all the objects from the bucket? If so, isn't that dangerous? Would it be good to have validation or something passed to the API by the user to do this?
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 did not touch change the behaviour of this API call. I only added the filtering in
valueToURLParams
(so only those parameters that are actually supported by the API according to the ceph-rgw source code are serialized to the query string).It is certainly a dangerous operation (even without purge objects, given that all obejcts that are only in this bucket will be deleted independently of the
purge-objects
setting), but the way it behaves does not change with this commit.With
purge-objects
set to true all the objects in the bucket will be deleted, before deleting the bucket (I guess that means they will also be deleted from other buckets that may contain them).For purge-objects to happen
*bucket.PurgeObjects
must be set to true, this will never be the case for Bucket objects returned from the API, so explicit action is necessary on the side of the user.