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

Clean up some duplicated code in cmd/ #3648

Merged
merged 6 commits into from
Jul 17, 2024
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
3 changes: 0 additions & 3 deletions cmd/aro/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ package main
// Licensed under the Apache License 2.0.

const (
envDatabaseName = "DATABASE_NAME"
envDatabaseAccountName = "DATABASE_ACCOUNT_NAME"
envKeyVaultPrefix = "KEYVAULT_PREFIX"
envOpenShiftVersions = "OPENSHIFT_VERSIONS"
envInstallerImageDigests = "INSTALLER_IMAGE_DIGESTS"
envPlatformWorkloadIdentityRoleSets = "PLATFORM_WORKLOAD_IDENTITY_ROLE_SETS"
Expand Down
21 changes: 2 additions & 19 deletions cmd/aro/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -35,28 +34,12 @@ func gateway(ctx context.Context, log *logrus.Entry) error {

go g.Run()

if err := env.ValidateVars(envDatabaseAccountName); err != nil {
return err
}

msiToken, err := _env.NewMSITokenCredential()
if err != nil {
return err
}
logrusEntry := log.WithField("component", "database")

dbAccountName := os.Getenv(envDatabaseAccountName)
scope := []string{fmt.Sprintf("https://%s.%s", dbAccountName, _env.Environment().CosmosDBDNSSuffixScope)}
dbAuthorizer, err := database.NewTokenAuthorizer(ctx, logrusEntry, msiToken, dbAccountName, scope)
if err != nil {
return err
}
dbc, err := database.NewDatabaseClient(logrusEntry, _env, dbAuthorizer, m, nil, dbAccountName)
dbc, err := database.NewDatabaseClientFromEnv(ctx, _env, log, m, nil)
if err != nil {
return err
}

dbName, err := DBName(_env.IsLocalDevelopmentMode())
dbName, err := env.DBName(_env)
if err != nil {
return err
}
Expand Down
13 changes: 0 additions & 13 deletions cmd/aro/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"os"
"strings"

"github.com/Azure/ARO-RP/pkg/env"
utillog "github.com/Azure/ARO-RP/pkg/util/log"
_ "github.com/Azure/ARO-RP/pkg/util/scheme"
"github.com/Azure/ARO-RP/pkg/util/version"
Expand Down Expand Up @@ -98,15 +97,3 @@ func checkMinArgs(required int) {
os.Exit(2)
}
}

func DBName(isLocalDevelopmentMode bool) (string, error) {
if !isLocalDevelopmentMode {
return "ARO", nil
}

if err := env.ValidateVars(envDatabaseName); err != nil {
return "", fmt.Errorf("%v (development mode)", err.Error())
}

return os.Getenv(envDatabaseName), nil
}
39 changes: 3 additions & 36 deletions cmd/aro/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"context"
"fmt"
"os"

"github.com/Azure/go-autorest/tracing"
Expand All @@ -22,7 +21,6 @@ import (
pkgmonitor "github.com/Azure/ARO-RP/pkg/monitor"
"github.com/Azure/ARO-RP/pkg/proxy"
"github.com/Azure/ARO-RP/pkg/util/encryption"
"github.com/Azure/ARO-RP/pkg/util/keyvault"
)

func monitor(ctx context.Context, log *logrus.Entry) error {
Expand Down Expand Up @@ -60,48 +58,17 @@ func monitor(ctx context.Context, log *logrus.Entry) error {

clusterm := statsd.New(ctx, log.WithField("component", "metrics"), _env, os.Getenv("CLUSTER_MDM_ACCOUNT"), os.Getenv("CLUSTER_MDM_NAMESPACE"), os.Getenv("MDM_STATSD_SOCKET"))

msiToken, err := _env.NewMSITokenCredential()
aead, err := encryption.NewAEADWithCore(ctx, _env, env.EncryptionSecretV2Name, env.EncryptionSecretName)
if err != nil {
return err
}

msiKVAuthorizer, err := _env.NewMSIAuthorizer(_env.Environment().KeyVaultScope)
dbc, err := database.NewDatabaseClientFromEnv(ctx, _env, log, &noop.Noop{}, aead)
if err != nil {
return err
}

if err := env.ValidateVars(envKeyVaultPrefix); err != nil {
return err
}
keyVaultPrefix := os.Getenv(envKeyVaultPrefix)
// TODO: should not be using the service keyvault here
serviceKeyvaultURI := keyvault.URI(_env, env.ServiceKeyvaultSuffix, keyVaultPrefix)
serviceKeyvault := keyvault.NewManager(msiKVAuthorizer, serviceKeyvaultURI)

aead, err := encryption.NewMulti(ctx, serviceKeyvault, env.EncryptionSecretV2Name, env.EncryptionSecretName)
if err != nil {
return err
}

if err := env.ValidateVars(envDatabaseAccountName); err != nil {
return err
}

dbAccountName := os.Getenv(envDatabaseAccountName)

logrusEntry := log.WithField("component", "database")
scope := []string{fmt.Sprintf("https://%s.%s", dbAccountName, _env.Environment().CosmosDBDNSSuffixScope)}
dbAuthorizer, err := database.NewTokenAuthorizer(ctx, logrusEntry, msiToken, dbAccountName, scope)
if err != nil {
return err
}

dbc, err := database.NewDatabaseClient(log.WithField("component", "database"), _env, dbAuthorizer, &noop.Noop{}, aead, dbAccountName)
if err != nil {
return err
}

dbName, err := DBName(_env.IsLocalDevelopmentMode())
dbName, err := env.DBName(_env)
if err != nil {
return err
}
Expand Down
40 changes: 7 additions & 33 deletions cmd/aro/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main
import (
"context"
"crypto/x509"
"fmt"
"net"
"os"
"strings"
Expand Down Expand Up @@ -61,16 +60,6 @@ func portal(ctx context.Context, log *logrus.Entry, audit *logrus.Entry) error {
return err
}

msiToken, err := _env.NewMSITokenCredential()
if err != nil {
return err
}

msiKVAuthorizer, err := _env.NewMSIAuthorizer(_env.Environment().KeyVaultScope)
if err != nil {
return err
}

m := statsd.New(ctx, log.WithField("component", "portal"), _env, os.Getenv("MDM_ACCOUNT"), os.Getenv("MDM_NAMESPACE"), os.Getenv("MDM_STATSD_SOCKET"))

g, err := golang.NewMetrics(log.WithField("component", "portal"), m)
Expand All @@ -80,52 +69,37 @@ func portal(ctx context.Context, log *logrus.Entry, audit *logrus.Entry) error {

go g.Run()

if err := env.ValidateVars(envKeyVaultPrefix); err != nil {
return err
}
keyVaultPrefix := os.Getenv(envKeyVaultPrefix)
// TODO: should not be using the service keyvault here
serviceKeyvaultURI := keyvault.URI(_env, env.ServiceKeyvaultSuffix, keyVaultPrefix)
serviceKeyvault := keyvault.NewManager(msiKVAuthorizer, serviceKeyvaultURI)

aead, err := encryption.NewMulti(ctx, serviceKeyvault, env.EncryptionSecretV2Name, env.EncryptionSecretName)
aead, err := encryption.NewAEADWithCore(ctx, _env, env.EncryptionSecretV2Name, env.EncryptionSecretName)
if err != nil {
return err
}

if err := env.ValidateVars(envDatabaseAccountName); err != nil {
return err
}

dbAccountName := os.Getenv(envDatabaseAccountName)

logrusEntry := log.WithField("component", "database")
scope := []string{fmt.Sprintf("https://%s.%s", dbAccountName, _env.Environment().CosmosDBDNSSuffixScope)}
dbAuthorizer, err := database.NewTokenAuthorizer(ctx, logrusEntry, msiToken, dbAccountName, scope)
dbc, err := database.NewDatabaseClientFromEnv(ctx, _env, log, m, aead)
if err != nil {
return err
}

dbc, err := database.NewDatabaseClient(log.WithField("component", "database"), _env, dbAuthorizer, m, aead, dbAccountName)
dbName, err := env.DBName(_env)
if err != nil {
return err
}

dbName, err := DBName(_env.IsLocalDevelopmentMode())
dbOpenShiftClusters, err := database.NewOpenShiftClusters(ctx, dbc, dbName)
if err != nil {
return err
}

dbOpenShiftClusters, err := database.NewOpenShiftClusters(ctx, dbc, dbName)
dbPortal, err := database.NewPortal(ctx, dbc, dbName)
if err != nil {
return err
}

dbPortal, err := database.NewPortal(ctx, dbc, dbName)
msiKVAuthorizer, err := _env.NewMSIAuthorizer(_env.Environment().KeyVaultScope)
if err != nil {
return err
}

keyVaultPrefix := os.Getenv(encryption.KeyVaultPrefix)
portalKeyvaultURI := keyvault.URI(_env, env.PortalKeyvaultSuffix, keyVaultPrefix)
portalKeyvault := keyvault.NewManager(msiKVAuthorizer, portalKeyvaultURI)

Expand Down
23 changes: 3 additions & 20 deletions cmd/aro/rp.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,17 @@ func rp(ctx context.Context, log, audit *logrus.Entry) error {

clusterm := statsd.New(ctx, log.WithField("component", "metrics"), _env, os.Getenv("CLUSTER_MDM_ACCOUNT"), os.Getenv("CLUSTER_MDM_NAMESPACE"), os.Getenv("MDM_STATSD_SOCKET"))

msiToken, err := _env.NewMSITokenCredential()
aead, err := encryption.NewAEADWithCore(ctx, _env, env.EncryptionSecretV2Name, env.EncryptionSecretName)
if err != nil {
return err
}

aead, err := encryption.NewMulti(ctx, _env.ServiceKeyvault(), env.EncryptionSecretV2Name, env.EncryptionSecretName)
dbc, err := database.NewDatabaseClientFromEnv(ctx, _env, log, metrics, aead)
if err != nil {
return err
}

if err := env.ValidateVars(envDatabaseAccountName); err != nil {
return err
}
dbAccountName := os.Getenv(envDatabaseAccountName)

logrusEntry := log.WithField("component", "database")
scope := []string{fmt.Sprintf("https://%s.%s", dbAccountName, _env.Environment().CosmosDBDNSSuffixScope)}
dbAuthorizer, err := database.NewTokenAuthorizer(ctx, logrusEntry, msiToken, dbAccountName, scope)
if err != nil {
return err
}

dbc, err := database.NewDatabaseClient(log.WithField("component", "database"), _env, dbAuthorizer, metrics, aead, dbAccountName)
if err != nil {
return err
}

dbName, err := DBName(env.IsLocalDevelopmentMode())
dbName, err := env.DBName(_env)
if err != nil {
return err
}
Expand Down
44 changes: 3 additions & 41 deletions cmd/aro/update_ocp_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ import (
"fmt"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy"
"github.com/sirupsen/logrus"

"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database"
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/metrics/statsd"
"github.com/Azure/ARO-RP/pkg/util/encryption"
"github.com/Azure/ARO-RP/pkg/util/keyvault"
"github.com/Azure/ARO-RP/pkg/util/version"
)

Expand Down Expand Up @@ -160,53 +157,18 @@ func getVersionsDatabase(ctx context.Context, log *logrus.Entry) (database.OpenS
}
}

msiToken, err := _env.NewMSITokenCredential()
if err != nil {
return nil, fmt.Errorf("MSI Authorizer failed with: %s", err.Error())
}

msiKVAuthorizer, err := _env.NewMSIAuthorizer(_env.Environment().KeyVaultScope)
if err != nil {
return nil, fmt.Errorf("MSI KeyVault Authorizer failed with: %s", err.Error())
}

m := statsd.New(ctx, log.WithField("component", "update-ocp-versions"), _env, os.Getenv("MDM_ACCOUNT"), os.Getenv("MDM_NAMESPACE"), os.Getenv("MDM_STATSD_SOCKET"))

if err := env.ValidateVars(envKeyVaultPrefix); err != nil {
return nil, err
}
keyVaultPrefix := os.Getenv(envKeyVaultPrefix)
serviceKeyvaultURI := keyvault.URI(_env, env.ServiceKeyvaultSuffix, keyVaultPrefix)
serviceKeyvault := keyvault.NewManager(msiKVAuthorizer, serviceKeyvaultURI)

aead, err := encryption.NewMulti(ctx, serviceKeyvault, env.EncryptionSecretV2Name, env.EncryptionSecretName)
dbc, err := database.NewDatabaseClientFromEnv(ctx, _env, log, m, nil)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed creating database client: %w", err)
}

if err := env.ValidateVars(envDatabaseAccountName); err != nil {
return nil, err
}

dbAccountName := os.Getenv(envDatabaseAccountName)
clientOptions := &policy.ClientOptions{
ClientOptions: _env.Environment().ManagedIdentityCredentialOptions().ClientOptions,
}
logrusEntry := log.WithField("component", "database")
dbAuthorizer, err := database.NewMasterKeyAuthorizer(ctx, logrusEntry, msiToken, clientOptions, _env.SubscriptionID(), _env.ResourceGroup(), dbAccountName)
if err != nil {
return nil, err
}

dbc, err := database.NewDatabaseClient(log.WithField("component", "database"), _env, dbAuthorizer, m, aead, dbAccountName)
dbName, err := env.DBName(_env)
if err != nil {
return nil, err
}

dbName, err := DBName(_env.IsLocalDevelopmentMode())
if err != nil {
return nil, err
}
dbOpenShiftVersions, err := database.NewOpenShiftVersions(ctx, dbc, dbName)
if err != nil {
return nil, err
Expand Down
25 changes: 8 additions & 17 deletions cmd/aro/update_role_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/metrics/statsd"
"github.com/Azure/ARO-RP/pkg/util/encryption"
"github.com/Azure/ARO-RP/pkg/util/keyvault"
)

func getRoleSetsFromEnv() ([]api.PlatformWorkloadIdentityRoleSetProperties, error) {
Expand All @@ -38,26 +37,23 @@ func getPlatformWorkloadIdentityRoleSetDatabase(ctx context.Context, log *logrus
return nil, fmt.Errorf("MSI Authorizer failed with: %s", err.Error())
}

msiKVAuthorizer, err := _env.NewMSIAuthorizer(_env.Environment().KeyVaultScope)
if err != nil {
return nil, fmt.Errorf("MSI KeyVault Authorizer failed with: %s", err.Error())
}

m := statsd.New(ctx, log.WithField("component", "update-role-sets"), _env, os.Getenv("MDM_ACCOUNT"), os.Getenv("MDM_NAMESPACE"), os.Getenv("MDM_STATSD_SOCKET"))

keyVaultPrefix := os.Getenv(envKeyVaultPrefix)
serviceKeyvaultURI := keyvault.URI(_env, env.ServiceKeyvaultSuffix, keyVaultPrefix)
serviceKeyvault := keyvault.NewManager(msiKVAuthorizer, serviceKeyvaultURI)
aead, err := encryption.NewAEADWithCore(ctx, _env, env.EncryptionSecretV2Name, env.EncryptionSecretName)
if err != nil {
return nil, err
}

aead, err := encryption.NewMulti(ctx, serviceKeyvault, env.EncryptionSecretV2Name, env.EncryptionSecretName)
dbName, err := env.DBName(_env)
if err != nil {
return nil, err
}

if err := env.ValidateVars(envDatabaseAccountName); err != nil {
dbAccountName, err := env.DBAccountName()
if err != nil {
return nil, err
}
dbAccountName := os.Getenv(envDatabaseAccountName)

clientOptions := &policy.ClientOptions{
ClientOptions: _env.Environment().ManagedIdentityCredentialOptions().ClientOptions,
}
Expand All @@ -73,11 +69,6 @@ func getPlatformWorkloadIdentityRoleSetDatabase(ctx context.Context, log *logrus
return nil, err
}

dbName, err := DBName(_env.IsLocalDevelopmentMode())
if err != nil {
return nil, err
}

return database.NewPlatformWorkloadIdentityRoleSets(ctx, dbc, dbName)
}

Expand Down
Loading
Loading