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

Added SDK calls for createAzApp #276

Closed
wants to merge 10 commits into from
44 changes: 23 additions & 21 deletions pkg/providers/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription"
"github.com/Azure/draft/pkg/cred"
msgraph "github.com/microsoftgraph/msgraph-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
"os/exec"
"time"

Expand Down Expand Up @@ -45,7 +49,7 @@ func InitiateAzureOIDCFlow(ctx context.Context, sc *SetUpCmd, s spinner.Spinner)

if AzAppExists(sc.AppName) {
return errors.New("app already exists")
} else if err := sc.createAzApp(); err != nil {
} else if err := sc.createAzApp(ctx); err != nil {
return err
}

Expand Down Expand Up @@ -85,38 +89,36 @@ func InitiateAzureOIDCFlow(ctx context.Context, sc *SetUpCmd, s spinner.Spinner)
return nil
}

func (sc *SetUpCmd) createAzApp() error {
func (sc *SetUpCmd) createAzApp(ctx context.Context) error {
log.Debug("Commencing Azure app creation...")
start := time.Now()
log.Debug(start)

createApp := func() error {
createAppCmd := exec.Command("az", "ad", "app", "create", "--only-show-errors", "--display-name", sc.AppName)

out, err := createAppCmd.CombinedOutput()
azCred, err := cred.GetCred()
if err != nil {
log.Printf("%s\n", out)
return err
return fmt.Errorf("getting credentials: %w", err)
}
graphClient, _ := msgraph.NewGraphServiceClientWithCredentials(azCred, []string{cloud.AzurePublic.Services[cloud.ResourceManager].Endpoint + "/.default"})
Vidya2606 marked this conversation as resolved.
Show resolved Hide resolved

if AzAppExists(sc.AppName) {
var azApp map[string]interface{}
if err := json.Unmarshal(out, &azApp); err != nil {
return err
}
appId := fmt.Sprint(azApp["appId"])

sc.appId = appId
requestBody := graphmodels.NewApplication()
displayName := sc.AppName
requestBody.SetDisplayName(&displayName)

end := time.Since(start)
log.Debug("App created successfully!")
log.Debug(end)
return nil
application, err := graphClient.Applications().Post(ctx, requestBody, nil)
if err != nil {
return fmt.Errorf("creating Azure app: %v", err)
}

return errors.New("app creation time has exceeded max elapsed time for exponential backoff")
}
appId := *application.GetId()

sc.appId = appId

end := time.Since(start)
log.Debug("App created successfully!")
log.Debug(end)
return nil
}
backoff := bo.NewExponentialBackOff()
backoff.MaxElapsedTime = 5 * time.Second

Expand Down
Loading