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

CLI-1374: Show service account resource ID in audit-log describe and connect event describe #1055

Merged
merged 4 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/confluentinc/cc-structs/kafka/scheduler v0.812.0
github.com/confluentinc/cc-structs/kafka/util v0.753.0
github.com/confluentinc/cc-structs/operator v0.753.0
github.com/confluentinc/ccloud-sdk-go-v1 v0.0.91
github.com/confluentinc/ccloud-sdk-go-v1 v0.0.92
github.com/confluentinc/countrycode v0.0.0-20210804214833-917e401d6677
github.com/confluentinc/go-editor v0.4.0
github.com/confluentinc/go-netrc v0.0.0-20201015001751-d8d220f17928
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ github.com/confluentinc/cc-structs/kafka/util v0.753.0/go.mod h1:zK+BAdNsqeeEbGW
github.com/confluentinc/cc-structs/operator v0.405.0/go.mod h1:h80UL1NR+Q9Ey/umropf7dg7/RvW2zqXhRvqvAJVgv4=
github.com/confluentinc/cc-structs/operator v0.753.0 h1:/7iehTE/W8bJrJ8A9b/JFhWovHGYoU6p60joR8sKNCo=
github.com/confluentinc/cc-structs/operator v0.753.0/go.mod h1:HuCEW8KtDHzJOUpikNgspO9orr70yiD0RZnzBxNLV/E=
github.com/confluentinc/ccloud-sdk-go-v1 v0.0.91 h1:DW5G4f8mJ+Vpc7Ay5gu9SKul+GCFkA9JVnqUVrdgItc=
github.com/confluentinc/ccloud-sdk-go-v1 v0.0.91/go.mod h1:A7vR8Me1rDKWnWADVGj5bweYIce4bzPQ+R05bZIAU90=
github.com/confluentinc/ccloud-sdk-go-v1 v0.0.92 h1:vTcLNIx+jStH1O6K0dYVqxDu5kosJZCJ3MzbD9KXuQ0=
github.com/confluentinc/ccloud-sdk-go-v1 v0.0.92/go.mod h1:A7vR8Me1rDKWnWADVGj5bweYIce4bzPQ+R05bZIAU90=
github.com/confluentinc/cire-bucket-service/protos/bucket v0.50.0 h1:MKQiWsLL0A6Ml0qRoSda5UFn1E3lMpqzjME06dCOlQI=
github.com/confluentinc/cire-bucket-service/protos/bucket v0.50.0/go.mod h1:Yqjcig48HUMh97OHvPxWGZMj5VXon8XIY+AKhZMj16U=
github.com/confluentinc/cire-k8saas-structs v1.19.0/go.mod h1:arP5z1yPnhZf+yglHM2K5fn0J1coT5bK1AzmflQQNDk=
Expand Down
26 changes: 16 additions & 10 deletions internal/cmd/audit-log/command_describe.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package auditlog

import (
"context"

"github.com/spf13/cobra"

pcmd "github.com/confluentinc/cli/internal/pkg/cmd"
Expand Down Expand Up @@ -29,11 +31,10 @@ type describeCmd struct {
}

type auditLogStruct struct {
ClusterId string
EnvironmentId string
ServiceAccountId int32
ServiceAccountResourceId string
TopicName string
ClusterId string
EnvironmentId string
ServiceAccountId string
TopicName string
}

func NewDescribeCommand(prerunner pcmd.PreRunner) *cobra.Command {
Expand All @@ -60,11 +61,16 @@ func (c describeCmd) describe(cmd *cobra.Command, _ []string) error {
}

auditLog := c.State.Auth.Organization.AuditLog

serviceAccount, err := c.Client.User.GetServiceAccount(context.Background(), auditLog.ServiceAccountId)
if err != nil {
return err
}

return output.DescribeObject(cmd, &auditLogStruct{
ClusterId: auditLog.ClusterId,
EnvironmentId: auditLog.AccountId,
ServiceAccountId: auditLog.ServiceAccountId,
ServiceAccountResourceId: auditLog.AccountId,
TopicName: auditLog.TopicName,
ClusterId: auditLog.ClusterId,
EnvironmentId: auditLog.AccountId,
ServiceAccountId: serviceAccount.ResourceId,
TopicName: auditLog.TopicName,
}, listFields, humanLabelMap, structuredLabelMap)
}
15 changes: 12 additions & 3 deletions internal/cmd/audit-log/command_describe_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package auditlog

import (
"context"
"testing"

"github.com/confluentinc/cli/internal/pkg/errors"

orgv1 "github.com/confluentinc/cc-structs/kafka/org/v1"
"github.com/confluentinc/ccloud-sdk-go-v1"
ccloudmock "github.com/confluentinc/ccloud-sdk-go-v1/mock"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"

pcmd "github.com/confluentinc/cli/internal/pkg/cmd"
v1 "github.com/confluentinc/cli/internal/pkg/config/v1"
"github.com/confluentinc/cli/internal/pkg/errors"
climock "github.com/confluentinc/cli/mock"
)

Expand All @@ -31,8 +32,16 @@ func TestAuditLogDescribeUnconfigured(t *testing.T) {
}

func mockAuditLogCommand(configured bool) *cobra.Command {
client := &ccloud.Client{}
client := &ccloud.Client{
User: &ccloudmock.User{
GetServiceAccountFunc: func(_ context.Context, id int32) (*orgv1.User, error) {
return &orgv1.User{ResourceId: "sa-123456"}, nil
},
},
}

cfg := v1.AuthenticatedCloudConfigMock()

if configured {
cfg.Context().State.Auth.Organization.AuditLog = &orgv1.AuditLog{
ClusterId: "lkc-ab123",
Expand Down
13 changes: 11 additions & 2 deletions internal/cmd/connect/command_event.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package connect

import (
"context"

"github.com/spf13/cobra"

pcmd "github.com/confluentinc/cli/internal/pkg/cmd"
Expand All @@ -15,7 +17,7 @@ type eventCommand struct {
type connectLogEventsInfo struct {
ClusterId string
EnvironmentId string
ServiceAccountId int32
ServiceAccountId string
TopicName string
}

Expand Down Expand Up @@ -68,11 +70,18 @@ func (c *eventCommand) describe(cmd *cobra.Command, _ []string) error {
c.State.Auth.Organization.AuditLog.ClusterId == "" {
return errors.New(errors.ConnectLogEventsNotEnabledErrorMsg)
}

auditLog := c.State.Auth.Organization.AuditLog

serviceAccount, err := c.Client.User.GetServiceAccount(context.Background(), auditLog.ServiceAccountId)
if err != nil {
return err
}

return output.DescribeObject(cmd, &connectLogEventsInfo{
ClusterId: auditLog.ClusterId,
EnvironmentId: auditLog.AccountId,
ServiceAccountId: auditLog.ServiceAccountId,
ServiceAccountId: serviceAccount.ResourceId,
TopicName: "confluent-connect-log-events",
}, connectLogListFields, humanLabelMap, structuredLabelMap)
}
14 changes: 0 additions & 14 deletions test/audit_log_describe_test.go

This file was deleted.

4 changes: 4 additions & 0 deletions test/audit_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
mds "github.com/confluentinc/mds-sdk-go/mdsv1"
)

func (s *CLITestSuite) TestAuditLogDescribe() {
s.runCcloudTest(CLITest{args: "audit-log describe", login: "default", fixture: "auditlog/describe.golden"})
}

func (s *CLITestSuite) TestAuditLogConfig() {
tests := []CLITest{
{
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/auditlog/describe.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
+-----------------+----------------------------+
| Cluster | lkc-ab123 |
| Environment | env-987zy |
| Service Account | 1337 |
| Service Account | sa-12345 |
| Topic Name | confluent-audit-log-events |
+-----------------+----------------------------+
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
+-----------------+------------------------------+
| Cluster | lkc-ab123 |
| Environment | env-987zy |
| Service Account | 1337 |
| Service Account | sa-12345 |
| Topic Name | confluent-connect-log-events |
+-----------------+------------------------------+
24 changes: 22 additions & 2 deletions test/test-server/ccloud_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package test_server
import (
"encoding/json"
"fmt"
"github.com/gogo/protobuf/proto"
"io"
"io/ioutil"
"net/http"
Expand All @@ -13,6 +12,7 @@ import (
"testing"
"time"

"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -284,7 +284,7 @@ func (c *CloudRouter) HandlePromoCodeClaims(t *testing.T) func(http.ResponseWrit
}

// Handler for: "/api/service_accounts"
func (c *CloudRouter) HandleServiceAccount(t *testing.T) func(http.ResponseWriter, *http.Request) {
func (c *CloudRouter) HandleServiceAccounts(t *testing.T) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
Expand Down Expand Up @@ -342,6 +342,26 @@ func (c *CloudRouter) HandleServiceAccount(t *testing.T) func(http.ResponseWrite
}
}

// Handler for: "/api/service_accounts/{id}"
func (c *CloudRouter) HandleServiceAccount(t *testing.T) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
idStr := mux.Vars(r)["id"]
_, err := strconv.Atoi(idStr)
require.NoError(t, err)

switch r.Method {
case "GET":
res := &orgv1.GetServiceAccountReply{User: &orgv1.User{ResourceId: serviceAccountResourceID}}

data, err := json.Marshal(res)
require.NoError(t, err)

_, err = w.Write(data)
require.NoError(t, err)
}
}
}

// Handler for: "/api/api_keys"
func (c *CloudRouter) HandleApiKeys(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
Expand Down
8 changes: 7 additions & 1 deletion test/test-server/cloud_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
clusters = "/api/clusters"
envMetadata = "/api/env_metadata"
serviceAccounts = "/api/service_accounts"
serviceAccount = "/api/service_accounts/{id}"
schemaRegistries = "/api/schema_registries"
schemaRegistry = "/api/schema_registries/{id}"
ksql = "/api/ksqls/{id}"
Expand Down Expand Up @@ -77,7 +78,6 @@ func (c *CloudRouter) buildCcloudRouter(t *testing.T) {
c.HandleFunc(signup, c.HandleSignup(t))
c.HandleFunc(verifyEmail, c.HandleSendVerificationEmail(t))
c.HandleFunc(envMetadata, c.HandleEnvMetadata(t))
c.HandleFunc(serviceAccounts, c.HandleServiceAccount(t))
c.addSchemaRegistryRoutes(t)
c.addEnvironmentRoutes(t)
c.addOrgRoutes(t)
Expand All @@ -89,6 +89,7 @@ func (c *CloudRouter) buildCcloudRouter(t *testing.T) {
c.addV2AlphaRoutes(t)
c.addUsageLimitRoutes(t)
c.addMetricsQueryRoutes(t)
c.addServiceAccountRoutes(t)
}

func (c CloudRouter) addV2AlphaRoutes(t *testing.T) {
Expand Down Expand Up @@ -164,3 +165,8 @@ func (c *CloudRouter) addMetricsQueryRoutes(t *testing.T) {
c.HandleFunc(metricsApi, c.HandleMetricsQuery(t))
c.HandleFunc(accessTokens, c.HandleJwtToken(t))
}

func (c *CloudRouter) addServiceAccountRoutes(t *testing.T) {
c.HandleFunc(serviceAccounts, c.HandleServiceAccounts(t))
c.HandleFunc(serviceAccount, c.HandleServiceAccount(t))
}