Skip to content
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 listRoles and dropRole to the client.go #726

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions pkg/httphelper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,78 @@ func (client *NodeMgmtClient) CallCreateRoleEndpoint(pod *corev1.Pod, username s
return nil
}

// Create a new superuser with the given username and password
burmanm marked this conversation as resolved.
Show resolved Hide resolved
func (client *NodeMgmtClient) CallDropRoleEndpoint(pod *corev1.Pod, username string) error {
burmanm marked this conversation as resolved.
Show resolved Hide resolved
client.Log.Info(
"calling Management API drop role - DELETE /api/v0/ops/auth/role",
"pod", pod.Name,
)

postData := url.Values{}
postData.Set("username", username)

podHost, podPort, err := BuildPodHostFromPod(pod)
if err != nil {
return err
}

request := nodeMgmtRequest{
endpoint: fmt.Sprintf("/api/v0/ops/auth/role?%s", postData.Encode()),
host: podHost,
port: podPort,
method: http.MethodDelete,
timeout: 60 * time.Second,
}

_, err = callNodeMgmtEndpoint(client, request, "")
return err
}

type User struct {
Name string `json:"name"`
Super string `json:"super"`
Login string `json:"login"`
Options string `json:"options"`
Datacenters string `json:"datacenters"`
}

func parseListRoles(body []byte) ([]User, error) {
var users []User
if err := json.Unmarshal(body, &users); err != nil {
fmt.Printf("Received an error: %v\n", err)
return nil, err
}
return users, nil
}

// Create a new superuser with the given username and password
burmanm marked this conversation as resolved.
Show resolved Hide resolved
func (client *NodeMgmtClient) CallListRolesEndpoint(pod *corev1.Pod) ([]User, error) {
client.Log.Info(
"calling Management API list roles - GET /api/v0/ops/auth/role",
"pod", pod.Name,
)

podHost, podPort, err := BuildPodHostFromPod(pod)
if err != nil {
return nil, err
}

request := nodeMgmtRequest{
endpoint: "/api/v0/ops/auth/role",
host: podHost,
port: podPort,
method: http.MethodGet,
timeout: 60 * time.Second,
}

content, err := callNodeMgmtEndpoint(client, request, "")
if err != nil {
return nil, err
}

return parseListRoles(content)
}

func (client *NodeMgmtClient) CallProbeClusterEndpoint(pod *corev1.Pod, consistencyLevel string, rfPerDc int) error {
client.Log.Info(
"calling Management API cluster health - GET /api/v0/probes/cluster",
Expand Down
40 changes: 32 additions & 8 deletions pkg/httphelper/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/go-logr/logr"
"github.com/k8ssandra/cass-operator/pkg/mocks"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -229,7 +230,7 @@ func TestNodeMgmtClient_GetKeyspaceReplication(t *testing.T) {
"success",
goodPod,
"ks1",
newMockHttpClient(newHttpResponse(successBody, http.StatusOK), nil),
newMockHttpClient(newHttpResponseMarshalled(successBody, http.StatusOK), nil),
successBody,
nil,
},
Expand Down Expand Up @@ -261,7 +262,7 @@ func TestNodeMgmtClient_GetKeyspaceReplication(t *testing.T) {
"keyspace not found",
goodPod,
"ks1",
newMockHttpClient(newHttpResponse("Keyspace 'ks1' does not exist", http.StatusNotFound), nil),
newMockHttpClient(newHttpResponseMarshalled("Keyspace 'ks1' does not exist", http.StatusNotFound), nil),
nil,
&RequestError{
StatusCode: http.StatusNotFound,
Expand Down Expand Up @@ -292,7 +293,7 @@ func TestNodeMgmtClient_ListTables(t *testing.T) {
"success",
goodPod,
"ks1",
newMockHttpClient(newHttpResponse([]string{"table1", "table2"}, http.StatusOK), nil),
newMockHttpClient(newHttpResponseMarshalled([]string{"table1", "table2"}, http.StatusOK), nil),
[]string{"table1", "table2"},
nil,
},
Expand Down Expand Up @@ -324,7 +325,7 @@ func TestNodeMgmtClient_ListTables(t *testing.T) {
"keyspace not found",
goodPod,
"ks1",
newMockHttpClient(newHttpResponse([]string{}, http.StatusOK), nil),
newMockHttpClient(newHttpResponseMarshalled([]string{}, http.StatusOK), nil),
[]string{},
nil,
},
Expand Down Expand Up @@ -361,7 +362,7 @@ func TestNodeMgmtClient_CreateTable(t *testing.T) {
"success",
goodPod,
goodTable,
newMockHttpClient(newHttpResponse("OK", http.StatusOK), nil),
newMockHttpClient(newHttpResponseMarshalled("OK", http.StatusOK), nil),
nil,
},
{
Expand Down Expand Up @@ -423,7 +424,7 @@ func TestNodeMgmtClient_CreateTable(t *testing.T) {
"table1",
NewPartitionKeyColumn("", "int", 0),
),
newMockHttpClient(newHttpResponse("Table creation failed: 'columns[0].name' must not be empty", http.StatusBadRequest), nil),
newMockHttpClient(newHttpResponseMarshalled("Table creation failed: 'columns[0].name' must not be empty", http.StatusBadRequest), nil),
&RequestError{
StatusCode: http.StatusBadRequest,
Err: errors.New("incorrect status code of 400 when calling endpoint"),
Expand All @@ -433,7 +434,7 @@ func TestNodeMgmtClient_CreateTable(t *testing.T) {
"keyspace not found",
goodPod,
goodTable,
newMockHttpClient(newHttpResponse("keyspace does not exist", http.StatusInternalServerError), nil),
newMockHttpClient(newHttpResponseMarshalled("keyspace does not exist", http.StatusInternalServerError), nil),
&RequestError{
StatusCode: http.StatusInternalServerError,
Err: errors.New("incorrect status code of 500 when calling endpoint"),
Expand All @@ -449,6 +450,19 @@ func TestNodeMgmtClient_CreateTable(t *testing.T) {
}
}

func TestListRoles(t *testing.T) {
require := require.New(t)
payload := []byte(`[{"datacenters":"ALL","login":"false","name":"try","options":"{}","super":"false"},{"datacenters":"ALL","login":"true","name":"cluster2-superuser","options":"{}","super":"true"},{"datacenters":"ALL","login":"false","name":"cassandra","options":"{}","super":"false"}]`)
roles, err := parseListRoles(payload)
require.NoError(err)
require.Equal(3, len(roles))

mgmtClient := newMockMgmtClient(newMockHttpClient(newHttpResponse(payload, http.StatusOK), nil))
roles, err = mgmtClient.CallListRolesEndpoint(goodPod)
require.NoError(err)
require.Equal(3, len(roles))
}

func newMockMgmtClient(httpClient *mocks.HttpClient) *NodeMgmtClient {
return &NodeMgmtClient{
Client: httpClient,
Expand All @@ -463,7 +477,17 @@ func newMockHttpClient(response *http.Response, err error) *mocks.HttpClient {
return httpClient
}

func newHttpResponse(responseBody interface{}, status int) *http.Response {
func newHttpResponse(responseBody []byte, status int) *http.Response {
body := io.NopCloser(bytes.NewReader(responseBody))
bodyLength := int64(len(responseBody))
return &http.Response{
StatusCode: status,
Body: body,
ContentLength: bodyLength,
}
}

func newHttpResponseMarshalled(responseBody interface{}, status int) *http.Response {
marshalled, _ := json.Marshal(responseBody)
body := io.NopCloser(bytes.NewReader(marshalled))
bodyLength := int64(len(marshalled))
Expand Down
Loading