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

Adds service_principal_object_id attribute to data_source_arm_client_config #175

Merged
merged 2 commits into from
Jul 20, 2017
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
16 changes: 16 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/dns"
"github.com/Azure/azure-sdk-for-go/arm/documentdb"
"github.com/Azure/azure-sdk-for-go/arm/eventhub"
"github.com/Azure/azure-sdk-for-go/arm/graphrbac"
"github.com/Azure/azure-sdk-for-go/arm/keyvault"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/Azure/azure-sdk-for-go/arm/redis"
Expand Down Expand Up @@ -112,6 +113,8 @@ type ArmClient struct {
sqlElasticPoolsClient sql.ElasticPoolsClient

appInsightsClient appinsights.ComponentsClient

servicePrincipalsClient graphrbac.ServicePrincipalsClient
}

func withRequestLogging() autorest.SendDecorator {
Expand Down Expand Up @@ -197,8 +200,15 @@ func (c *Config) getArmClient() (*ArmClient, error) {
return nil, err
}

graphSpt, err := adal.NewServicePrincipalToken(*oauthConfig, c.ClientID, c.ClientSecret, env.GraphEndpoint)
if err != nil {
return nil, err
}

endpoint := env.ResourceManagerEndpoint
auth := autorest.NewBearerAuthorizer(spt)
graphEndpoint := env.GraphEndpoint
graphAuth := autorest.NewBearerAuthorizer(graphSpt)

// NOTE: these declarations should be left separate for clarity should the
// clients be wished to be configured with custom Responders/PollingModess etc...
Expand Down Expand Up @@ -508,6 +518,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
ai.Sender = autorest.CreateSender(withRequestLogging())
client.appInsightsClient = ai

spc := graphrbac.NewServicePrincipalsClientWithBaseURI(graphEndpoint, c.TenantID)
setUserAgent(&spc.Client)
spc.Authorizer = graphAuth
spc.Sender = autorest.CreateSender(withRequestLogging())
client.servicePrincipalsClient = spc

return &client, nil
}

Expand Down
22 changes: 22 additions & 0 deletions azurerm/data_source_arm_client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package azurerm
import (
"time"

"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

Expand All @@ -23,17 +25,37 @@ func dataSourceArmClientConfig() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"service_principal_object_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceArmClientConfigRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
spClient := client.servicePrincipalsClient
// Application & Service Principal is 1:1 per tenant. Since we know the appId (client_id)
// here, we can query for the Service Principal whose appId matches.
filter := fmt.Sprintf("appId eq '%s'", client.clientId)
listResult, listErr := spClient.List(filter)

if listErr != nil {
return fmt.Errorf("Error listing Service Principals: %#v", listErr)
}

if listResult.Value == nil || len(*listResult.Value) != 1 {
return fmt.Errorf("Unexpected Service Principal query result: %#v", listResult.Value)
}

servicePrincipal := (*listResult.Value)[0]

d.SetId(time.Now().UTC().String())
d.Set("client_id", client.clientId)
d.Set("tenant_id", client.tenantId)
d.Set("subscription_id", client.subscriptionId)
d.Set("service_principal_object_id", *servicePrincipal.ObjectID)

return nil
}
19 changes: 19 additions & 0 deletions azurerm/data_source_arm_client_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"testing"

"regexp"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
Expand All @@ -23,6 +25,7 @@ func TestAccAzureRMClientConfig_basic(t *testing.T) {
testAzureRMClientConfigAttr("data.azurerm_client_config.current", "client_id", clientId),
testAzureRMClientConfigAttr("data.azurerm_client_config.current", "tenant_id", tenantId),
testAzureRMClientConfigAttr("data.azurerm_client_config.current", "subscription_id", subscriptionId),
testAzureRMClientConfigGUIDAttr("data.azurerm_client_config.current", "service_principal_object_id"),
),
},
},
Expand All @@ -43,6 +46,22 @@ func testAzureRMClientConfigAttr(name, key, value string) resource.TestCheckFunc
}
}

func testAzureRMClientConfigGUIDAttr(name, key string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r, err := regexp.Compile("^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make more sense to use this method in the go.uuid library (which is already vendored) for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it is easier this way because I don't have to extract the attribute value from state myself?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair point - it's not a blocker by any means - just seeing if we can reuse the UUID parser if it made sense :)

if err != nil {
return err
}

err = resource.TestMatchResourceAttr(name, key, r)(s)
if err != nil {
return err
}

return nil
}
}

const testAccCheckArmClientConfig_basic = `
data "azurerm_client_config" "current" { }
`
Loading