Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
cmd/deploy: Handle error due to missing permissions during deploy (#2297
Browse files Browse the repository at this point in the history
)

* Handle error due to missing permissions during deploy

* CreateRoleAssignmentSimple can already return an error. Use this if a status 403 (not enough permissions) occurs.
  This is opposed to status 404 that seems to be issued to signal work in progress during service principal generation (by arm).
* autoFillApimodel: remove the duplicated retry logic of CreateRoleAssignmentSimple. this allows to properly fail if CreateRoleAssignmentSimple returns an error

* style fix: gofmt -s
  • Loading branch information
vvvrrooomm authored and jackfrancis committed Feb 20, 2018
1 parent e5774d7 commit bd362d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
13 changes: 5 additions & 8 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,11 @@ func autofillApimodel(dc *deployCmd) {
log.Warnf("created application with applicationID (%s) and servicePrincipalObjectID (%s).", applicationID, servicePrincipalObjectID)

log.Warnln("apimodel: ServicePrincipalProfile was empty, assigning role to application...")
for {
err = dc.client.CreateRoleAssignmentSimple(dc.resourceGroup, servicePrincipalObjectID)
if err != nil {
log.Debugf("Failed to create role assignment (will retry): %q", err)
time.Sleep(3 * time.Second)
continue
}
break

err = dc.client.CreateRoleAssignmentSimple(dc.resourceGroup, servicePrincipalObjectID)
if err != nil {
log.Fatalf("apimodel: could not create or assign ServicePrincipal: %q", err)

}

dc.containerService.Properties.ServicePrincipalProfile = &api.ServicePrincipalProfile{
Expand Down
11 changes: 9 additions & 2 deletions pkg/armhelpers/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package armhelpers

import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/arm/authorization"
"github.com/Azure/azure-sdk-for-go/arm/graphrbac"
"github.com/Azure/go-autorest/autorest/date"
"github.com/Azure/go-autorest/autorest/to"
"github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"regexp"
"time"
)

const (
Expand Down Expand Up @@ -97,13 +97,20 @@ func (az *AzureClient) CreateRoleAssignmentSimple(resourceGroup, servicePrincipa
},
}

re := regexp.MustCompile("(?i)status=(\\d+)")
for {
_, err := az.CreateRoleAssignment(
scope,
roleAssignmentName,
roleAssignmentParameters,
)
if err != nil {
match := re.FindStringSubmatch(err.Error())
if match != nil && (match[1] == "403") {
//insufficient permissions. stop now
log.Debugf("Failed to create role assignment (will abort now): %q", err)
return err
}
log.Debugf("Failed to create role assignment (will retry): %q", err)
time.Sleep(3 * time.Second)
continue
Expand Down

0 comments on commit bd362d9

Please sign in to comment.