Skip to content

Commit

Permalink
sp creation ids
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgamero committed Nov 22, 2024
1 parent 25637b3 commit b97bf1a
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 81 deletions.
29 changes: 6 additions & 23 deletions pkg/prompts/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/validation"

"github.com/Azure/draft/pkg/config"
)
Expand Down Expand Up @@ -141,28 +142,10 @@ func NoBlankStringValidator(s string) error {

// Validator for App name
func appNameValidator(name string) error {
if name == "" {
return fmt.Errorf("application name cannot be empty")
errors := validation.IsDNS1123Label(name)
if errors != nil {
return fmt.Errorf("invalid app name: %s", strings.Join(errors, ", "))
}

if !unicode.IsLetter(rune(name[0])) && !unicode.IsDigit(rune(name[0])) {
return fmt.Errorf("application name must start with a letter or digit")
}

if name[len(name)-1] == '-' || name[len(name)-1] == '_' || name[len(name)-1] == '.' {
return fmt.Errorf("application name must end with a letter or digit")
}

for _, r := range name {
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '_' && r != '.' {
return fmt.Errorf("application name can only contain letters, digits, '-', '_', and '.'")
}
}

if len(name) > 63 {
return fmt.Errorf("application name cannot be longer than 63 characters")
}

return nil
}

Expand Down Expand Up @@ -312,9 +295,9 @@ func getCurrentDirName() (string, error) {
func sanitizeAppName(name string) string {
var builder strings.Builder

// Remove all characters except alphanumeric, '-', '_', '.'
// Remove all characters except alphanumeric, '-', '.'
for _, r := range name {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' || r == '_' || r == '.' {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' || r == '.' {
builder.WriteRune(r)
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/providers/az-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ type AzClientInterface interface {
AzAcrExists(acrName string) bool
AzAksExists(aksName string, resourceGroup string) bool
AzAppExists(appName string) bool
CreateAzApp(appName string) error
CreateServicePrincipal(appId string) error
CreateAzApp(appName string) (string, error)
CreateServicePrincipal(appId string) (string, error)
EnsureAzCli()
EnsureAzCliLoggedIn()
GetAzCliVersion() (string, error)
GetAzSubscriptionLabels() ([]SubLabel, error)
GetAzUpgrade() string
GetCurrentAzSubscriptionLabel() (SubLabel, error)
GetServicePrincipal(appId string) (string, error)
IsLoggedInToAz() bool
IsSubscriptionIdValid(subscriptionId string) error
IsValidResourceGroup(subscriptionId string, resourceGroup string) error
ListResourceGroups(ctx context.Context, subscriptionID string) ([]armresources.ResourceGroup, error)
ListTenants(ctx context.Context) ([]armsubscription.TenantIDDescription, error)
LogInToAz() error
ServicePrincipalExists(appId string) bool
UpgradeAzCli()
ValidateAzCliInstalled() error
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/providers/azcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (az *AzClient) ValidateAzCliInstalled() error {
func (az *AzClient) IsLoggedInToAz() bool {
log.Debug("Checking that user is logged in to Azure CLI...")
_, err := az.CommandRunner.RunCommand("az", "ad", "signed-in-user", "show", "--only-show-errors", "--query", "objectId")
return err != nil
return err != nil
}

func (az *AzClient) EnsureAzCliLoggedIn() {
Expand Down Expand Up @@ -173,17 +173,17 @@ func (az *AzClient) AzAppExists(appName string) bool {
return len(azApp) >= 1
}

func (az *AzClient) ServicePrincipalExists(appId string) bool {
func (az *AzClient) GetServicePrincipal(appId string) (string, error) {
out, err := az.CommandRunner.RunCommand("az", "ad", "sp", "show", "--only-show-errors", "--id", appId, "--query", "id")
if err != nil {
return false
return "", err
}

var objectId string
json.Unmarshal([]byte(out), &objectId)

log.Debugf("Service principal with appId '%s' exists", appId)
return true
return objectId, nil
}

func (az *AzClient) AzAcrExists(acrName string) bool {
Expand Down
45 changes: 30 additions & 15 deletions pkg/providers/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ func InitiateAzureOIDCFlow(ctx context.Context, sc *SetUpCmd, s spinner.Spinner,
}

if !az.AzAppExists(sc.AppName) {
err := az.CreateAzApp(sc.AppName)
appId, err := az.CreateAzApp(sc.AppName)
if err != nil {
return err
}
sc.appId = appId
}

if err := az.CreateServicePrincipal(sc.appId); err != nil {
spObjId, err := az.CreateServicePrincipal(sc.appId)
if err != nil {
return err
}
sc.spObjectId = spObjId

if err := sc.getAppObjectId(); err != nil {
return err
Expand Down Expand Up @@ -80,10 +83,13 @@ func InitiateAzureOIDCFlow(ctx context.Context, sc *SetUpCmd, s spinner.Spinner,
return nil
}

func (az *AzClient) CreateAzApp(appName string) error {
// CreateAzApp creates an Azure app with the given name
// Returns the appId of the created app
func (az *AzClient) CreateAzApp(appName string) (string, error) {
log.Debug("Commencing Azure app creation...")
start := time.Now()
log.Debug(start)
createdAppId := ""

createApp := func() error {
out, err := az.CommandRunner.RunCommand("az", "ad", "app", "create", "--only-show-errors", "--display-name", appName)
Expand All @@ -97,7 +103,7 @@ func (az *AzClient) CreateAzApp(appName string) error {
if err := json.Unmarshal([]byte(out), &azApp); err != nil {
return err
}
createdAppId := fmt.Sprint(azApp["appId"])
createdAppId = fmt.Sprint(azApp["appId"])

end := time.Since(start)
log.Debugf("App with appId '%s' created successfully!", createdAppId)
Expand All @@ -114,17 +120,24 @@ func (az *AzClient) CreateAzApp(appName string) error {
err := bo.Retry(createApp, backoff)
if err != nil {
log.Debug(err)
return err
return "", err
}

return nil
return createdAppId, nil
}

func (az *AzClient) CreateServicePrincipal(appId string) error {
// CreateServicePrincipal creates a service principal with the given appId
// Returns the objectId of the created service principal
func (az *AzClient) CreateServicePrincipal(appId string) (string, error) {
log.Debug("creating Azure service principal...")
start := time.Now()
log.Debug(start)

if appId == "" {
return "", errors.New("appId cannot be empty")
}
createdObjectId := ""

createServicePrincipal := func() error {
out, err := az.CommandRunner.RunCommand("az", "ad", "sp", "create", "--id", appId, "--only-show-errors")
if err != nil {
Expand All @@ -133,14 +146,16 @@ func (az *AzClient) CreateServicePrincipal(appId string) error {
}

log.Debug("checking sp was created...")
if az.ServicePrincipalExists(appId) {
log.Debug("Service principal created successfully!")
end := time.Since(start)
log.Debug(end)
return nil
spObjId, err := az.GetServicePrincipal(appId)
if err != nil {
return errors.New("service principal not found")
}
log.Debug("Service principal created successfully!")
end := time.Since(start)
log.Debug(end)
createdObjectId = spObjId
return nil

return errors.New("service principal not found")
}

backoff := bo.NewExponentialBackOff()
Expand All @@ -149,10 +164,10 @@ func (az *AzClient) CreateServicePrincipal(appId string) error {
err := bo.Retry(createServicePrincipal, backoff)
if err != nil {
log.Debug(err)
return err
return "", err
}

return nil
return createdObjectId, nil
}

// Prompt the user to select a tenant ID if there are multiple tenants, or return the only tenant ID if there is only one
Expand Down
2 changes: 0 additions & 2 deletions test/integration/clojure/helm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "8-jdk-alpine"
Expand Down
2 changes: 0 additions & 2 deletions test/integration/clojure/kustomize.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "8-jdk-alpine"
Expand Down
2 changes: 0 additions & 2 deletions test/integration/clojure/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "8-jdk-alpine"
Expand Down
2 changes: 0 additions & 2 deletions test/integration/gradle/helm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "11-jre"
Expand Down
2 changes: 0 additions & 2 deletions test/integration/gradle/kustomize.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "11-jre"
Expand Down
2 changes: 0 additions & 2 deletions test/integration/gradle/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "11-jre"
Expand Down
2 changes: 0 additions & 2 deletions test/integration/java/helm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "11-jre"
Expand Down
2 changes: 0 additions & 2 deletions test/integration/java/kustomize.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "11-jre"
Expand Down
2 changes: 0 additions & 2 deletions test/integration/java/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "11-jre"
Expand Down
6 changes: 0 additions & 6 deletions test/integration/swift/helm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "CPULIMIT"
value: "3"
- name: "MEMLIMIT"
value: "2Gi"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "5.5"
Expand Down
6 changes: 0 additions & 6 deletions test/integration/swift/kustomize.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "CPULIMIT"
value: "3"
- name: "MEMLIMIT"
value: "2Gi"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "5.5"
Expand Down
6 changes: 0 additions & 6 deletions test/integration/swift/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ deployVariables:
value: "testapp"
- name: "IMAGENAME"
value: "host.minikube.internal:5001/testapp"
- name: "CPULIMIT"
value: "3"
- name: "MEMLIMIT"
value: "2Gi"
- name: "STARTUPINITIALDELAY"
value: 30
languageVariables:
- name: "VERSION"
value: "5.5"
Expand Down

0 comments on commit b97bf1a

Please sign in to comment.