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

fix(cluster connect): print access commands separately for services #1327

Merged
merged 4 commits into from
Nov 18, 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
4 changes: 4 additions & 0 deletions docs/commands/rhoas_cluster_connect.adoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions pkg/cluster/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ func (api *KubernetesClusterAPIImpl) ExecuteConnect(connectOpts *v1alpha.Connect
return kubeclient.TranslatedKubernetesErrors(api.CommandEnvironment, err)
}

err = api.createServiceAccountSecretIfNeeded(currentNamespace)
clientID, err := api.createServiceAccountSecretIfNeeded(currentNamespace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

if err != nil {
return kubeclient.TranslatedKubernetesErrors(api.CommandEnvironment, err)
}

if clientID != "" {
currentService.PrintAccessCommands(clientID)
}

err = api.createCustomResource(serviceDetails, currentNamespace)
if err != nil {
return kubeclient.TranslatedKubernetesErrors(api.CommandEnvironment, err)
Expand Down Expand Up @@ -181,20 +185,20 @@ func (c *KubernetesClusterAPIImpl) createTokenSecretIfNeeded(namespace string, a
}

// createSecret creates a new secret to store the SASL/PLAIN credentials from the service account
func (c *KubernetesClusterAPIImpl) createServiceAccountSecretIfNeeded(namespace string) error {
func (c *KubernetesClusterAPIImpl) createServiceAccountSecretIfNeeded(namespace string) (string, error) {
cliOpts := c.CommandEnvironment
kClients := c.KubernetesClients
ctx := cliOpts.Context

_, err := kClients.Clientset.CoreV1().Secrets(namespace).Get(context.TODO(), constants.ServiceAccountSecretName, metav1.GetOptions{})
if err == nil {
cliOpts.Logger.Info(cliOpts.Localizer.MustLocalize("cluster.kubernetes.serviceaccountsecret.log.info.exist"))
return nil
return "", nil
}

serviceAcct, err := c.createServiceAccount(ctx, cliOpts)
if err != nil {
return err
return "", err
}

secret := &apiv1.Secret{
Expand All @@ -210,15 +214,15 @@ func (c *KubernetesClusterAPIImpl) createServiceAccountSecretIfNeeded(namespace

createdSecret, err := kClients.Clientset.CoreV1().Secrets(namespace).Create(cliOpts.Context, secret, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("%v: %w", cliOpts.Localizer.MustLocalize("cluster.kubernetes.serviceaccountsecret.error.createError"), err)
return "", fmt.Errorf("%v: %w", cliOpts.Localizer.MustLocalize("cluster.kubernetes.serviceaccountsecret.error.createError"), err)
}

cliOpts.Logger.Info(icon.SuccessPrefix(), cliOpts.Localizer.MustLocalize("cluster.kubernetes.createSASecret.log.info.createSuccess",
localize.NewEntry("Name", createdSecret.Name),
localize.NewEntry("ClientID", serviceAcct.GetClientId()),
))

return nil
return serviceAcct.GetClientId(), nil
}

// createServiceAccount creates a service account
Expand Down
3 changes: 3 additions & 0 deletions pkg/cluster/services/custom_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ type RHOASKubernetesService interface {

// Build Custom Resource representing desired service that should be created
BuildServiceDetails(serviceName string, namespace string, ignoreConfigContext bool) (*ServiceDetails, error)

// Print commands to be executed to grant access to the service
PrintAccessCommands(clientID string)
}
7 changes: 7 additions & 0 deletions pkg/cluster/services/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/cluster/services/resources"
"github.com/redhat-developer/app-services-cli/pkg/cluster/v1alpha"
"github.com/redhat-developer/app-services-cli/pkg/kafka"
"github.com/redhat-developer/app-services-cli/pkg/localize"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -78,3 +79,9 @@ func (s KafkaService) BuildServiceDetails(serviceName string, namespace string,

return &serviceDetails, nil
}

// PrintAccessCommands prints command to grant service account acccess to the Kafka instance
func (s KafkaService) PrintAccessCommands(clientID string) {
rkpattnaik780 marked this conversation as resolved.
Show resolved Hide resolved
rkpattnaik780 marked this conversation as resolved.
Show resolved Hide resolved
cliOpts := s.CommandEnvironment
cliOpts.Logger.Info(cliOpts.Localizer.MustLocalize("cluster.kubernetes.printKafkaAccessCommands", localize.NewEntry("ClientID", clientID)))
}
7 changes: 7 additions & 0 deletions pkg/cluster/services/service-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/cluster/kubeclient"
"github.com/redhat-developer/app-services-cli/pkg/cluster/services/resources"
"github.com/redhat-developer/app-services-cli/pkg/cluster/v1alpha"
"github.com/redhat-developer/app-services-cli/pkg/localize"
"github.com/redhat-developer/app-services-cli/pkg/serviceregistry"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -79,3 +80,9 @@ func (s RegistryService) BuildServiceDetails(serviceName string, namespace strin

return &serviceDetails, nil
}

// PrintAccessCommands prints command to assign service account roles in the service registry instance
func (s RegistryService) PrintAccessCommands(clientID string) {
cliOpts := s.CommandEnvironment
cliOpts.Logger.Info(cliOpts.Localizer.MustLocalize("cluster.kubernetes.printRegistryAccessCommands", localize.NewEntry("ClientID", clientID)))
}
18 changes: 16 additions & 2 deletions pkg/localize/locales/en/cmd/cluster.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ For example for Kafka service you should execute the following command to grant

$ rhoas kafka acl grant-access --producer --consumer --service-account your-sa --topic all --group all

Similarly for service registry you should execute the following command:

$ rhoas service-registry role add --role=DEVELOPER --service-account your-sa

'''

[cluster.connect.cmd.example]
Expand Down Expand Up @@ -301,11 +305,21 @@ Client ID: {{.ClientID}}
Make a copy of the client ID to store in a safe place. Credentials won't appear again after closing the terminal.

You will need to assign permissions to service account in order to use it.
For example for Kafka service you should execute the following command to grant access to the service account:
'''

[cluster.kubernetes.printKafkaAccessCommands]
one = '''
You need to separately grant service account access to Kafka by issuing following command

$ rhoas kafka acl grant-access --producer --consumer --service-account {{.ClientID}} --topic all --group all
'''


[cluster.kubernetes.printRegistryAccessCommands]
one = '''
You might need to assign non-readonly roles for the service account based on the use case (READ_ONLY by default):

$ rhoas service-registry role add --role=DEVELOPER --service-account {{.ClientID}}
'''

[cluster.kubernetes.createTokenSecret.log.info.createFailed]
one = 'Creation of the "{{.Name}}" secret failed:'
Expand Down