Skip to content

Commit

Permalink
fix: revert service accounts integration (#1732)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki authored Aug 23, 2022
1 parent c60f1fc commit ae637b8
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 74 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.13.0
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.5.1
github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.10.0
github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.8.0
github.com/redhat-developer/service-binding-operator v0.9.0
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
Expand Down
1 change: 0 additions & 1 deletion internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ var (
DefaultLoginTimeout = 60 * time.Second
OfflineTokenURL = ConsoleURL + "/openshift/token"
ProductionAuthURL = "https://sso.redhat.com/auth/realms/redhat-external"
StagingAuthURL = "https://sso.stage.redhat.com/auth/realms/redhat-external"
)

func init() {
Expand Down
13 changes: 6 additions & 7 deletions pkg/cmd/connector/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
connectorerror "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/error"
svcacctmgmtclient "github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt/apiv1/client"

"gopkg.in/AlecAivazis/survey.v1"

"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
connectormgmtclient "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/client"

kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -138,17 +137,17 @@ func runCreate(opts *options) error {
return nil
}

func createServiceAccount(opts *factory.Factory, shortDescription string) (*svcacctmgmtclient.ServiceAccountData, error) {
func createServiceAccount(opts *factory.Factory, shortDescription string) (*kafkamgmtclient.ServiceAccount, error) {
conn, err := opts.Connection()
if err != nil {
return nil, err
}
serviceAccountPayload := svcacctmgmtclient.ServiceAccountCreateRequestData{Name: shortDescription}
serviceAccountPayload := kafkamgmtclient.ServiceAccountRequest{Name: shortDescription}

serviceacct, httpRes, err := conn.API().
ServiceAccountMgmt().
CreateServiceAccount(opts.Context).
ServiceAccountCreateRequestData(serviceAccountPayload).
ServiceAccountRequest(serviceAccountPayload).
Execute()

if httpRes != nil {
Expand All @@ -159,7 +158,7 @@ func createServiceAccount(opts *factory.Factory, shortDescription string) (*svca
return nil, err
}
opts.Logger.Info(opts.Localizer.MustLocalize("connector.sa.created",
localize.NewEntry("ClientId", serviceacct.ClientId), localize.NewEntry("ClientSecret", serviceacct.Secret), localize.NewEntry("Name", shortDescription)))
localize.NewEntry("ClientId", serviceacct.ClientId), localize.NewEntry("ClientSecret", serviceacct.ClientSecret), localize.NewEntry("Name", shortDescription)))
return &serviceacct, nil
}

Expand All @@ -184,7 +183,7 @@ func setDefaultValuesFromFlags(connector *connectormgmtclient.ConnectorRequest,
if err1 != nil {
return err1
}
connector.ServiceAccount = *connectormgmtclient.NewServiceAccount(serviceAccount.GetClientId(), serviceAccount.GetSecret())
connector.ServiceAccount = *connectormgmtclient.NewServiceAccount(serviceAccount.GetClientId(), serviceAccount.GetClientSecret())
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ var apiGatewayAliases = map[string]string{
var authURLAliases = map[string]string{
"production": build.ProductionAuthURL,
"prod": build.ProductionAuthURL,
"staging": build.StagingAuthURL,
"stage": build.StagingAuthURL,
"staging": build.ProductionAuthURL,
"stage": build.ProductionAuthURL,
}

type options struct {
Expand Down
15 changes: 10 additions & 5 deletions pkg/cmd/serviceaccount/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"

svcacctmgmtclient "github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt/apiv1/client"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"

"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -127,12 +127,17 @@ func runCreate(opts *options) error {
spinner.SetSuffix(opts.localizer.MustLocalize("serviceAccount.create.log.info.creating"))
spinner.Start()
// create the service account
serviceAccountPayload := svcacctmgmtclient.ServiceAccountCreateRequestData{Name: opts.shortDescription}
serviceAccountPayload := kafkamgmtclient.ServiceAccountRequest{Name: opts.shortDescription}

providerUrls, err := svcaccountcmdutil.GetProvidersDetails(conn, opts.Context)
if err != nil {
return err
}

serviceacct, httpRes, err := conn.API().
ServiceAccountMgmt().
CreateServiceAccount(opts.Context).
ServiceAccountCreateRequestData(serviceAccountPayload).
ServiceAccountRequest(serviceAccountPayload).
Execute()
spinner.Stop()

Expand All @@ -148,8 +153,8 @@ func runCreate(opts *options) error {

creds := &credentials.Credentials{
ClientID: serviceacct.GetClientId(),
ClientSecret: serviceacct.GetSecret(),
TokenURL: conn.API().GetConfig().AuthURL.String() + "/protocol/openid-connect/token",
ClientSecret: serviceacct.GetClientSecret(),
TokenURL: providerUrls.GetTokenUrl(),
}

// save the credentials to a file
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/serviceaccount/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func runDelete(opts *options) (err error) {
return err
}

_, httpRes, err := conn.API().ServiceAccountMgmt().GetServiceAccount(opts.Context, opts.id).Execute()
_, httpRes, err := conn.API().ServiceAccountMgmt().GetServiceAccountById(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func deleteServiceAccount(opts *options) error {
return err
}

httpRes, err := conn.API().ServiceAccountMgmt().DeleteServiceAccount(opts.Context, opts.id).Execute()
_, httpRes, err := conn.API().ServiceAccountMgmt().DeleteServiceAccountById(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/serviceaccount/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func runDescribe(opts *options) error {

api := conn.API()

res, httpRes, err := api.ServiceAccountMgmt().GetServiceAccount(opts.Context, opts.id).Execute()
res, httpRes, err := api.ServiceAccountMgmt().GetServiceAccountById(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
Expand Down
21 changes: 6 additions & 15 deletions pkg/cmd/serviceaccount/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package list

import (
"context"
"fmt"
"time"

"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/config"
Expand All @@ -12,8 +10,7 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
svcacctmgmtclient "github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt/apiv1/client"

kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -77,11 +74,12 @@ func runList(opts *options) (err error) {
return err
}

serviceaccounts, _, err := conn.API().ServiceAccountMgmt().GetServiceAccounts(opts.Context).Execute()
res, _, err := conn.API().ServiceAccountMgmt().GetServiceAccounts(opts.Context).Execute()
if err != nil {
return err
}

serviceaccounts := res.GetItems()
if len(serviceaccounts) == 0 && opts.output == "" {
opts.Logger.Info(opts.localizer.MustLocalize("serviceAccount.list.log.info.noneFound"))
return nil
Expand All @@ -93,33 +91,26 @@ func runList(opts *options) (err error) {
rows := mapResponseItemsToRows(serviceaccounts)
dump.Table(outStream, rows)
default:
return dump.Formatted(opts.IO.Out, opts.output, serviceaccounts)
return dump.Formatted(opts.IO.Out, opts.output, res)
}

return nil
}

func mapResponseItemsToRows(svcAccts []svcacctmgmtclient.ServiceAccountData) []svcAcctRow {
func mapResponseItemsToRows(svcAccts []kafkamgmtclient.ServiceAccountListItem) []svcAcctRow {
rows := make([]svcAcctRow, len(svcAccts))

for i, sa := range svcAccts {

row := svcAcctRow{
ID: sa.GetId(),
Name: sa.GetName(),
ClientID: sa.GetClientId(),
Owner: sa.GetCreatedBy(),
CreatedAt: unixTimestampToUTC(sa.GetCreatedAt()),
CreatedAt: sa.GetCreatedAt().String(),
}

rows[i] = row
}

return rows
}

// unixTimestampToUTC converts a unix timestamp to the corresponding local Time
func unixTimestampToUTC(timestamp int64) string {
localTime := time.Unix(timestamp, 0)
return fmt.Sprint(localTime)
}
27 changes: 13 additions & 14 deletions pkg/cmd/serviceaccount/resetcredentials/reset_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
svcacctmgmtclient "github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt/apiv1/client"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"

"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -107,19 +107,16 @@ func runResetCredentials(opts *options) (err error) {

api := conn.API()

if opts.id != "" {
_, httpRes, newErr := api.ServiceAccountMgmt().GetServiceAccount(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
if newErr != nil {
return newErr
}
_, httpRes, err := api.ServiceAccountMgmt().GetServiceAccountById(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}

if err != nil {
return err
}

if opts.interactive {

err = runInteractivePrompt(opts)
if err != nil {
return err
Expand Down Expand Up @@ -159,10 +156,12 @@ func runResetCredentials(opts *options) (err error) {

opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("serviceAccount.resetCredentials.log.info.resetSuccess", localize.NewEntry("ID", updatedServiceAccount.GetId())))

providerUrls, err := svcaccountcmdutil.GetProvidersDetails(conn, opts.Context)

creds := &credentials.Credentials{
ClientID: updatedServiceAccount.GetClientId(),
ClientSecret: updatedServiceAccount.GetSecret(),
TokenURL: conn.API().GetConfig().AuthURL.String() + "/protocol/openid-connect/token",
ClientSecret: updatedServiceAccount.GetClientSecret(),
TokenURL: providerUrls.GetTokenUrl(),
}

// save the credentials to a file
Expand All @@ -179,7 +178,7 @@ func runResetCredentials(opts *options) (err error) {
return nil
}

func resetCredentials(opts *options) (*svcacctmgmtclient.ServiceAccountData, error) {
func resetCredentials(opts *options) (*kafkamgmtclient.ServiceAccount, error) {
conn, err := opts.Connection()
if err != nil {
return nil, err
Expand All @@ -190,7 +189,7 @@ func resetCredentials(opts *options) (*svcacctmgmtclient.ServiceAccountData, err

opts.Logger.Debug(opts.localizer.MustLocalize("serviceAccount.resetCredentials.log.debug.resettingCredentials", localize.NewEntry("ID", opts.id)))

serviceacct, httpRes, err := api.ServiceAccountMgmt().ResetServiceAccountSecret(opts.Context, opts.id).Execute()
serviceacct, httpRes, err := api.ServiceAccountMgmt().ResetServiceAccountCreds(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/cmd/serviceaccount/svcaccountcmdutil/svcaccount_util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
package svcaccountcmdutil

import (
"context"

"github.com/redhat-developer/app-services-cli/pkg/cmd/serviceaccount/svcaccountcmdutil/credentials"
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"

kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
)

var (
CredentialsOutputFormats = []string{credentials.EnvFormat, credentials.JSONFormat, credentials.PropertiesFormat, credentials.SecretFormat}
)

// Method fetches authentication details for providers
func GetProvidersDetails(conn connection.Connection, context context.Context) (*kafkamgmtclient.SsoProvider, error) {
providers, httpRes, err := conn.API().
ServiceAccountMgmt().GetSsoProviders(context).Execute()

if httpRes != nil {
defer httpRes.Body.Close()
}

if err != nil {
return nil, err
}

return &providers, err
}
6 changes: 4 additions & 2 deletions pkg/core/cmdutil/flagutil/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ func RegisterServiceAccountCompletionFunc(cmd *cobra.Command, f *factory.Factory
return cachedServiceAccounts, directive
}

serviceAccounts, _, err := conn.API().ServiceAccountMgmt().GetServiceAccounts(cmd.Context()).Execute()
if err != nil || len(serviceAccounts) == 0 {
serviceAccountResults, _, err := conn.API().ServiceAccountMgmt().GetServiceAccounts(cmd.Context()).Execute()
if err != nil || len(serviceAccountResults.GetItems()) == 0 {
return emptyList, directive
}

serviceAccounts := serviceAccountResults.GetItems()

for _, serviceAcct := range serviceAccounts {
cachedServiceAccounts = append(cachedServiceAccounts, serviceAcct.GetClientId())
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/shared/cluster/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/golang-jwt/jwt/v4"
"github.com/redhat-developer/app-services-cli/internal/build"
svcacctmgmtclient "github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt/apiv1/client"

kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -68,7 +67,7 @@ func (api *KubernetesClusterAPIImpl) ExecuteConnect(connectOpts *v1alpha.Connect
localize.NewEntry("Namespace", color.Info(currentNamespace)),
localize.NewEntry("ServiceAccountSecretName", color.Info(constants.ServiceAccountSecretName))))

if !connectOpts.ForceCreationWithoutAsk {
if connectOpts.ForceCreationWithoutAsk == false {
var shouldContinue bool
confirm := &survey.Confirm{
Message: cliOpts.Localizer.MustLocalize("cluster.kubernetes.input.confirm.message"),
Expand Down Expand Up @@ -209,7 +208,7 @@ func (c *KubernetesClusterAPIImpl) createServiceAccountSecretIfNeeded(namespace
},
StringData: map[string]string{
"client-id": serviceAcct.GetClientId(),
"client-secret": serviceAcct.GetSecret(),
"client-secret": serviceAcct.GetClientSecret(),
},
}

Expand All @@ -227,13 +226,13 @@ func (c *KubernetesClusterAPIImpl) createServiceAccountSecretIfNeeded(namespace
}

// createServiceAccount creates a service account
func (c *KubernetesClusterAPIImpl) createServiceAccount(ctx context.Context, cliOpts *v1alpha.CommandEnvironment) (*svcacctmgmtclient.ServiceAccountData, error) {
func (c *KubernetesClusterAPIImpl) createServiceAccount(ctx context.Context, cliOpts *v1alpha.CommandEnvironment) (*kafkamgmtclient.ServiceAccount, error) {
t := time.Now()

api := cliOpts.Connection.API()
serviceAcct := &svcacctmgmtclient.ServiceAccountCreateRequestData{Name: fmt.Sprintf("rhoascli-%v", t.Unix())}
serviceAcct := &kafkamgmtclient.ServiceAccountRequest{Name: fmt.Sprintf("rhoascli-%v", t.Unix())}
req := api.ServiceAccountMgmt().CreateServiceAccount(ctx)
req = req.ServiceAccountCreateRequestData(*serviceAcct)
req = req.ServiceAccountRequest(*serviceAcct)
serviceAcctRes, httpRes, err := req.Execute()
if httpRes != nil {
defer httpRes.Body.Close()
Expand Down
Loading

0 comments on commit ae637b8

Please sign in to comment.