diff --git a/azurerm/data_source_azuread_application.go b/azurerm/data_source_azuread_application.go index 8d7727d6cdf3..dc2e5011fe83 100644 --- a/azurerm/data_source_azuread_application.go +++ b/azurerm/data_source_azuread_application.go @@ -14,7 +14,6 @@ func dataSourceArmAzureADApplication() *schema.Resource { Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, - // TODO: customizeDiff for validation of either name or object_id. Schema: map[string]*schema.Schema{ "object_id": { diff --git a/azurerm/data_source_azuread_application_test.go b/azurerm/data_source_azuread_application_test.go index 4b28ab07d65e..451fe9c2a804 100644 --- a/azurerm/data_source_azuread_application_test.go +++ b/azurerm/data_source_azuread_application_test.go @@ -18,6 +18,9 @@ func TestAccDataSourceAzureRMAzureADApplication_byObjectId(t *testing.T) { Providers: testAccProviders, CheckDestroy: testCheckAzureRMActiveDirectoryApplicationDestroy, Steps: []resource.TestStep{ + { + Config: testAccAzureRMActiveDirectoryApplication_basic(id), + }, { Config: config, Check: resource.ComposeTestCheckFunc( @@ -37,15 +40,16 @@ func TestAccDataSourceAzureRMAzureADApplication_byObjectId(t *testing.T) { func TestAccDataSourceAzureRMAzureADApplication_byObjectIdComplete(t *testing.T) { dataSourceName := "data.azurerm_azuread_application.test" id := uuid.New().String() - config := testAccDataSourceAzureRMAzureADApplication_objectIdComplete(id) - resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMActiveDirectoryApplicationDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: testAccAzureRMActiveDirectoryApplication_complete(id), + }, + { + Config: testAccDataSourceAzureRMAzureADApplication_objectIdComplete(id), Check: resource.ComposeTestCheckFunc( testCheckAzureRMActiveDirectoryApplicationExists(dataSourceName), resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest%s", id)), diff --git a/azurerm/data_source_azuread_service_principal.go b/azurerm/data_source_azuread_service_principal.go new file mode 100644 index 000000000000..b26822a4b2fc --- /dev/null +++ b/azurerm/data_source_azuread_service_principal.go @@ -0,0 +1,111 @@ +package azurerm + +import ( + "fmt" + + "github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmActiveDirectoryServicePrincipal() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmActiveDirectoryServicePrincipalRead, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "object_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ConflictsWith: []string{"display_name", "application_id"}, + }, + + "display_name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ConflictsWith: []string{"object_id", "application_id"}, + }, + + "application_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ConflictsWith: []string{"object_id", "display_name"}, + }, + }, + } +} + +func dataSourceArmActiveDirectoryServicePrincipalRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).servicePrincipalsClient + ctx := meta.(*ArmClient).StopContext + + var servicePrincipal *graphrbac.ServicePrincipal + + if v, ok := d.GetOk("object_id"); ok { + objectId := v.(string) + app, err := client.Get(ctx, objectId) + if err != nil { + if utils.ResponseWasNotFound(app.Response) { + return fmt.Errorf("Service Principal with Object ID %q was not found!", objectId) + } + + return fmt.Errorf("Error retrieving Service Principal ID %q: %+v", objectId, err) + } + + servicePrincipal = &app + } else { + apps, err := client.ListComplete(ctx, "") + if err != nil { + return fmt.Errorf("Error listing Service Principals: %+v", err) + } + + if v, ok := d.GetOk("display_name"); ok { + displayName := v.(string) + + for _, app := range *apps.Response().Value { + if app.DisplayName == nil { + continue + } + + if *app.DisplayName == displayName { + servicePrincipal = &app + break + } + } + + if servicePrincipal == nil { + return fmt.Errorf("A Service Principal with the Display Name %q was not found", displayName) + } + } else { + applicationId := d.Get("application_id").(string) + + for _, app := range *apps.Response().Value { + if app.AppID == nil { + continue + } + + if *app.AppID == applicationId { + servicePrincipal = &app + break + } + } + + if servicePrincipal == nil { + return fmt.Errorf("A Service Principal for Application ID %q was not found", applicationId) + } + } + } + + d.SetId(*servicePrincipal.ObjectID) + + d.Set("application_id", servicePrincipal.AppID) + d.Set("display_name", servicePrincipal.DisplayName) + d.Set("object_id", servicePrincipal.ObjectID) + + return nil +} diff --git a/azurerm/data_source_azuread_service_principal_test.go b/azurerm/data_source_azuread_service_principal_test.go new file mode 100644 index 000000000000..a1ef2b857b98 --- /dev/null +++ b/azurerm/data_source_azuread_service_principal_test.go @@ -0,0 +1,111 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/google/uuid" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAzureRMAzureADServicePrincipal_byApplicationId(t *testing.T) { + dataSourceName := "data.azurerm_azuread_service_principal.test" + id := uuid.New().String() + config := testAccDataSourceAzureRMAzureADServicePrincipal_byApplicationId(id) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMActiveDirectoryServicePrincipalDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMActiveDirectoryServicePrincipalExists(dataSourceName), + resource.TestCheckResourceAttrSet(dataSourceName, "application_id"), + resource.TestCheckResourceAttrSet(dataSourceName, "object_id"), + resource.TestCheckResourceAttrSet(dataSourceName, "display_name"), + ), + }, + }, + }) +} + +func TestAccDataSourceAzureRMAzureADServicePrincipal_byDisplayName(t *testing.T) { + dataSourceName := "data.azurerm_azuread_service_principal.test" + id := uuid.New().String() + config := testAccDataSourceAzureRMAzureADServicePrincipal_byDisplayName(id) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMActiveDirectoryServicePrincipalDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMActiveDirectoryServicePrincipalExists(dataSourceName), + resource.TestCheckResourceAttrSet(dataSourceName, "application_id"), + resource.TestCheckResourceAttrSet(dataSourceName, "object_id"), + resource.TestCheckResourceAttrSet(dataSourceName, "display_name"), + ), + }, + }, + }) +} + +func TestAccDataSourceAzureRMAzureADServicePrincipal_byObjectId(t *testing.T) { + dataSourceName := "data.azurerm_azuread_service_principal.test" + id := uuid.New().String() + config := testAccDataSourceAzureRMAzureADServicePrincipal_byObjectId(id) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMActiveDirectoryServicePrincipalDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMActiveDirectoryServicePrincipalExists(dataSourceName), + resource.TestCheckResourceAttrSet(dataSourceName, "application_id"), + resource.TestCheckResourceAttrSet(dataSourceName, "object_id"), + resource.TestCheckResourceAttrSet(dataSourceName, "display_name"), + ), + }, + }, + }) +} + +func testAccDataSourceAzureRMAzureADServicePrincipal_byApplicationId(id string) string { + template := testAccAzureRMActiveDirectoryServicePrincipal_basic(id) + return fmt.Sprintf(` +%s + +data "azurerm_azuread_service_principal" "test" { + application_id = "${azurerm_azuread_service_principal.test.application_id}" +} +`, template) +} + +func testAccDataSourceAzureRMAzureADServicePrincipal_byDisplayName(id string) string { + template := testAccAzureRMActiveDirectoryServicePrincipal_basic(id) + return fmt.Sprintf(` +%s + +data "azurerm_azuread_service_principal" "test" { + display_name = "${azurerm_azuread_service_principal.test.display_name}" +} +`, template) +} + +func testAccDataSourceAzureRMAzureADServicePrincipal_byObjectId(id string) string { + template := testAccAzureRMActiveDirectoryServicePrincipal_basic(id) + return fmt.Sprintf(` +%s + +data "azurerm_azuread_service_principal" "test" { + object_id = "${azurerm_azuread_service_principal.test.id}" +} +`, template) +} diff --git a/azurerm/helpers/validate/uuid.go b/azurerm/helpers/validate/uuid.go new file mode 100644 index 000000000000..e4820c569d1a --- /dev/null +++ b/azurerm/helpers/validate/uuid.go @@ -0,0 +1,21 @@ +package validate + +import ( + "fmt" + + "github.com/hashicorp/go-uuid" +) + +func UUID(i interface{}, k string) (_ []string, errors []error) { + v, ok := i.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected type of %q to be string", k)) + return + } + + if _, err := uuid.ParseUUID(v); err != nil { + errors = append(errors, fmt.Errorf("%q isn't a valid UUID (%q): %+v", k, v, err)) + } + + return +} diff --git a/azurerm/helpers/validate/uuid_test.go b/azurerm/helpers/validate/uuid_test.go new file mode 100644 index 000000000000..15f3a5ea9cd0 --- /dev/null +++ b/azurerm/helpers/validate/uuid_test.go @@ -0,0 +1,37 @@ +package validate + +import "testing" + +func TestUUID(t *testing.T) { + cases := []struct { + Input string + Errors int + }{ + { + Input: "", + Errors: 1, + }, + { + Input: "hello-world", + Errors: 1, + }, + { + Input: "00000000-0000-111-0000-000000000000", + Errors: 1, + }, + { + Input: "00000000-0000-0000-0000-000000000000", + Errors: 0, + }, + } + + for _, tc := range cases { + t.Run(tc.Input, func(t *testing.T) { + _, errors := UUID(tc.Input, "test") + + if len(errors) != tc.Errors { + t.Fatalf("Expected UUID to have %d not %d errors for %q", tc.Errors, len(errors), tc.Input) + } + }) + } +} diff --git a/azurerm/import_arm_azuread_service_principal_test.go b/azurerm/import_arm_azuread_service_principal_test.go new file mode 100644 index 000000000000..015fd997cb35 --- /dev/null +++ b/azurerm/import_arm_azuread_service_principal_test.go @@ -0,0 +1,31 @@ +package azurerm + +import ( + "testing" + + "github.com/google/uuid" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAzureRMActiveDirectoryServicePrincipal_importBasic(t *testing.T) { + resourceName := "azurerm_azuread_service_principal.test" + + id := uuid.New().String() + config := testAccAzureRMActiveDirectoryServicePrincipal_basic(id) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMActiveDirectoryServicePrincipalDestroy, + Steps: []resource.TestStep{ + { + Config: config, + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index cc9f0d98927d..38f66315cbcc 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -79,6 +79,7 @@ func Provider() terraform.ResourceProvider { DataSourcesMap: map[string]*schema.Resource{ "azurerm_azuread_application": dataSourceArmAzureADApplication(), + "azurerm_azuread_service_principal": dataSourceArmActiveDirectoryServicePrincipal(), "azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(), "azurerm_app_service": dataSourceArmAppService(), "azurerm_app_service_plan": dataSourceAppServicePlan(), @@ -121,6 +122,8 @@ func Provider() terraform.ResourceProvider { ResourcesMap: map[string]*schema.Resource{ "azurerm_azuread_application": resourceArmActiveDirectoryApplication(), + "azurerm_azuread_service_principal": resourceArmActiveDirectoryServicePrincipal(), + "azurerm_azuread_service_principal_password": resourceArmActiveDirectoryServicePrincipalPassword(), "azurerm_application_gateway": resourceArmApplicationGateway(), "azurerm_application_insights": resourceArmApplicationInsights(), "azurerm_application_security_group": resourceArmApplicationSecurityGroup(), diff --git a/azurerm/resource_arm_azuread_service_principal.go b/azurerm/resource_arm_azuread_service_principal.go new file mode 100644 index 000000000000..71beabf650b3 --- /dev/null +++ b/azurerm/resource_arm_azuread_service_principal.go @@ -0,0 +1,101 @@ +package azurerm + +import ( + "fmt" + "log" + + "github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +var servicePrincipalResourceName = "azurerm_service_principal" + +func resourceArmActiveDirectoryServicePrincipal() *schema.Resource { + return &schema.Resource{ + Create: resourceArmActiveDirectoryServicePrincipalCreate, + Read: resourceArmActiveDirectoryServicePrincipalRead, + Delete: resourceArmActiveDirectoryServicePrincipalDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "application_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "display_name": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceArmActiveDirectoryServicePrincipalCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).servicePrincipalsClient + ctx := meta.(*ArmClient).StopContext + + applicationId := d.Get("application_id").(string) + + properties := graphrbac.ServicePrincipalCreateParameters{ + AppID: utils.String(applicationId), + // there's no way of retrieving this, and there's no way of changing it + // given there's no way to change it - we'll just default this to true + AccountEnabled: utils.Bool(true), + } + + app, err := client.Create(ctx, properties) + if err != nil { + return fmt.Errorf("Error creating Service Principal %q: %+v", applicationId, err) + } + + objectId := *app.ObjectID + resp, err := client.Get(ctx, objectId) + if err != nil { + return fmt.Errorf("Error retrieving Service Principal ID %q: %+v", objectId, err) + } + + d.SetId(*resp.ObjectID) + + return resourceArmActiveDirectoryServicePrincipalRead(d, meta) +} + +func resourceArmActiveDirectoryServicePrincipalRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).servicePrincipalsClient + ctx := meta.(*ArmClient).StopContext + + objectId := d.Id() + app, err := client.Get(ctx, objectId) + if err != nil { + if utils.ResponseWasNotFound(app.Response) { + log.Printf("[DEBUG] Service Principal with Object ID %q was not found - removing from state!", objectId) + d.SetId("") + return nil + } + return fmt.Errorf("Error retrieving Service Principal ID %q: %+v", objectId, err) + } + + d.Set("application_id", app.AppID) + d.Set("display_name", app.DisplayName) + + return nil +} + +func resourceArmActiveDirectoryServicePrincipalDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).servicePrincipalsClient + ctx := meta.(*ArmClient).StopContext + + applicationId := d.Id() + app, err := client.Delete(ctx, applicationId) + if err != nil { + if !response.WasNotFound(app.Response) { + return fmt.Errorf("Error deleting Service Principal ID %q: %+v", applicationId, err) + } + } + + return nil +} diff --git a/azurerm/resource_arm_azuread_service_principal_password.go b/azurerm/resource_arm_azuread_service_principal_password.go new file mode 100644 index 000000000000..f1c3bdf3f57c --- /dev/null +++ b/azurerm/resource_arm_azuread_service_principal_password.go @@ -0,0 +1,241 @@ +package azurerm + +import ( + "fmt" + "log" + "strings" + "time" + + "github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" + "github.com/Azure/go-autorest/autorest/date" + "github.com/hashicorp/go-uuid" + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func resourceArmActiveDirectoryServicePrincipalPassword() *schema.Resource { + return &schema.Resource{ + Create: resourceArmActiveDirectoryServicePrincipalPasswordCreate, + Read: resourceArmActiveDirectoryServicePrincipalPasswordRead, + Delete: resourceArmActiveDirectoryServicePrincipalPasswordDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "service_principal_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.UUID, + }, + + "key_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ValidateFunc: validate.UUID, + }, + + "value": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Sensitive: true, + }, + + "start_date": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ValidateFunc: validate.RFC3339Time, + }, + + "end_date": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.RFC3339Time, + }, + }, + } +} + +func resourceArmActiveDirectoryServicePrincipalPasswordCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).servicePrincipalsClient + ctx := meta.(*ArmClient).StopContext + + objectId := d.Get("service_principal_id").(string) + value := d.Get("value").(string) + // errors will be handled by the validation + endDate, _ := time.Parse(time.RFC3339, d.Get("end_date").(string)) + + var keyId string + if v, ok := d.GetOk("key_id"); ok { + keyId = v.(string) + } else { + kid, err := uuid.GenerateUUID() + if err != nil { + return err + } + + keyId = kid + } + + credential := graphrbac.PasswordCredential{ + KeyID: utils.String(keyId), + Value: utils.String(value), + EndDate: &date.Time{Time: endDate}, + } + + if v, ok := d.GetOk("start_date"); ok { + // errors will be handled by the validation + startDate, _ := time.Parse(time.RFC3339, v.(string)) + credential.StartDate = &date.Time{Time: startDate} + } + + azureRMLockByName(objectId, servicePrincipalResourceName) + defer azureRMUnlockByName(objectId, servicePrincipalResourceName) + + existingCredentials, err := client.ListPasswordCredentials(ctx, objectId) + if err != nil { + return fmt.Errorf("Error Listing Password Credentials for Service Principal %q: %+v", objectId, err) + } + + updatedCredentials := make([]graphrbac.PasswordCredential, 0) + if existingCredentials.Value != nil { + updatedCredentials = *existingCredentials.Value + } + + updatedCredentials = append(updatedCredentials, credential) + + parameters := graphrbac.PasswordCredentialsUpdateParameters{ + Value: &updatedCredentials, + } + _, err = client.UpdatePasswordCredentials(ctx, objectId, parameters) + if err != nil { + return fmt.Errorf("Error creating Password Credential %q for Service Principal %q: %+v", keyId, objectId, err) + } + + d.SetId(fmt.Sprintf("%s/%s", objectId, keyId)) + + return resourceArmActiveDirectoryServicePrincipalPasswordRead(d, meta) +} + +func resourceArmActiveDirectoryServicePrincipalPasswordRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).servicePrincipalsClient + ctx := meta.(*ArmClient).StopContext + + id := strings.Split(d.Id(), "/") + if len(id) != 2 { + return fmt.Errorf("ID should be in the format {objectId}/{keyId} - but got %q", d.Id()) + } + + objectId := id[0] + keyId := id[1] + + // ensure the parent Service Principal exists + servicePrincipal, err := client.Get(ctx, objectId) + if err != nil { + // the parent Service Principal has been removed - skip it + if utils.ResponseWasNotFound(servicePrincipal.Response) { + log.Printf("[DEBUG] Service Principal with Object ID %q was not found - removing from state!", objectId) + d.SetId("") + return nil + } + return fmt.Errorf("Error retrieving Service Principal ID %q: %+v", objectId, err) + } + + credentials, err := client.ListPasswordCredentials(ctx, objectId) + if err != nil { + return fmt.Errorf("Error Listing Password Credentials for Service Principal with Object ID %q: %+v", objectId, err) + } + + var credential *graphrbac.PasswordCredential + for _, c := range *credentials.Value { + if c.KeyID == nil { + continue + } + + if *c.KeyID == keyId { + credential = &c + break + } + } + + if credential == nil { + log.Printf("[DEBUG] Service Principal Password %q (Object ID %q) was not found - removing from state!", keyId, objectId) + d.SetId("") + return nil + } + + // value is available in the SDK but isn't returned from the API + d.Set("key_id", credential.KeyID) + d.Set("service_principal_id", objectId) + + if endDate := credential.EndDate; endDate != nil { + d.Set("end_date", endDate.Format(time.RFC3339)) + } + + if startDate := credential.StartDate; startDate != nil { + d.Set("start_date", startDate.Format(time.RFC3339)) + } + + return nil +} + +func resourceArmActiveDirectoryServicePrincipalPasswordDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).servicePrincipalsClient + ctx := meta.(*ArmClient).StopContext + + id := strings.Split(d.Id(), "/") + if len(id) != 2 { + return fmt.Errorf("ID should be in the format {objectId}/{keyId} - but got %q", d.Id()) + } + + objectId := id[0] + keyId := id[1] + + azureRMLockByName(objectId, servicePrincipalResourceName) + defer azureRMUnlockByName(objectId, servicePrincipalResourceName) + + // ensure the parent Service Principal exists + servicePrincipal, err := client.Get(ctx, objectId) + if err != nil { + // the parent Service Principal was removed - skip it + if utils.ResponseWasNotFound(servicePrincipal.Response) { + return nil + } + + return fmt.Errorf("Error retrieving Service Principal ID %q: %+v", objectId, err) + } + + existing, err := client.ListPasswordCredentials(ctx, objectId) + if err != nil { + return fmt.Errorf("Error Listing Password Credentials for Service Principal with Object ID %q: %+v", objectId, err) + } + + updatedCredentials := make([]graphrbac.PasswordCredential, 0) + for _, credential := range *existing.Value { + if credential.KeyID == nil { + continue + } + + if *credential.KeyID != keyId { + updatedCredentials = append(updatedCredentials, credential) + } + } + + parameters := graphrbac.PasswordCredentialsUpdateParameters{ + Value: &updatedCredentials, + } + _, err = client.UpdatePasswordCredentials(ctx, objectId, parameters) + if err != nil { + return fmt.Errorf("Error removing Password %q from Service Principal %q: %+v", keyId, objectId, err) + } + + return nil +} diff --git a/azurerm/resource_arm_azuread_service_principal_password_test.go b/azurerm/resource_arm_azuread_service_principal_password_test.go new file mode 100644 index 000000000000..386b0c9d5679 --- /dev/null +++ b/azurerm/resource_arm_azuread_service_principal_password_test.go @@ -0,0 +1,157 @@ +package azurerm + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/go-uuid" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func TestAccAzureRMActiveDirectoryServicePrincipalPassword_basic(t *testing.T) { + resourceName := "azurerm_azuread_service_principal_password.test" + applicationId, err := uuid.GenerateUUID() + if err != nil { + t.Fatal(err) + } + value, err := uuid.GenerateUUID() + if err != nil { + t.Fatal(err) + } + + config := testAccAzureRMActiveDirectoryServicePrincipalPassword_basic(applicationId, value) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMActiveDirectoryServicePrincipalDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + // can't assert on Value since it's not returned + testCheckAzureRMActiveDirectoryServicePrincipalPasswordExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "start_date"), + resource.TestCheckResourceAttrSet(resourceName, "key_id"), + resource.TestCheckResourceAttr(resourceName, "end_date", "2020-01-01T01:02:03Z"), + ), + }, + }, + }) +} + +func TestAccAzureRMActiveDirectoryServicePrincipalPassword_customKeyId(t *testing.T) { + resourceName := "azurerm_azuread_service_principal_password.test" + applicationId, err := uuid.GenerateUUID() + if err != nil { + t.Fatal(err) + } + keyId, err := uuid.GenerateUUID() + if err != nil { + t.Fatal(err) + } + value, err := uuid.GenerateUUID() + if err != nil { + t.Fatal(err) + } + config := testAccAzureRMActiveDirectoryServicePrincipalPassword_customKeyId(applicationId, keyId, value) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMActiveDirectoryServicePrincipalDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + // can't assert on Value since it's not returned + testCheckAzureRMActiveDirectoryServicePrincipalPasswordExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "start_date"), + resource.TestCheckResourceAttr(resourceName, "key_id", keyId), + resource.TestCheckResourceAttr(resourceName, "end_date", "2020-01-01T01:02:03Z"), + ), + }, + }, + }) +} + +func testCheckAzureRMActiveDirectoryServicePrincipalPasswordExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %q", name) + } + + client := testAccProvider.Meta().(*ArmClient).servicePrincipalsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + id := strings.Split(rs.Primary.ID, "/") + objectId := id[0] + keyId := id[1] + resp, err := client.Get(ctx, objectId) + + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Bad: Azure AD Service Principal %q does not exist", objectId) + } + return fmt.Errorf("Bad: Get on Azure AD servicePrincipalsClient: %+v", err) + } + + credentials, err := client.ListPasswordCredentials(ctx, objectId) + if err != nil { + return fmt.Errorf("Error Listing Password Credentials for Service Principal %q: %+v", objectId, err) + } + + for _, credential := range *credentials.Value { + if credential.KeyID == nil { + continue + } + + if *credential.KeyID == keyId { + return nil + } + } + + return fmt.Errorf("Password Credential %q was not found in Service Principal %q", keyId, objectId) + } +} + +func testAccAzureRMActiveDirectoryServicePrincipalPassword_basic(applicationId, value string) string { + return fmt.Sprintf(` +resource "azurerm_azuread_application" "test" { + name = "acctestspa%s" +} + +resource "azurerm_azuread_service_principal" "test" { + application_id = "${azurerm_azuread_application.test.application_id}" +} + +resource "azurerm_azuread_service_principal_password" "test" { + service_principal_id = "${azurerm_azuread_service_principal.test.id}" + value = "%s" + end_date = "2020-01-01T01:02:03Z" +} +`, applicationId, value) +} + +func testAccAzureRMActiveDirectoryServicePrincipalPassword_customKeyId(applicationId, keyId, value string) string { + return fmt.Sprintf(` +resource "azurerm_azuread_application" "test" { + name = "acctestspa%s" +} + +resource "azurerm_azuread_service_principal" "test" { + application_id = "${azurerm_azuread_application.test.application_id}" +} + +resource "azurerm_azuread_service_principal_password" "test" { + service_principal_id = "${azurerm_azuread_service_principal.test.id}" + key_id = "%s" + value = "%s" + end_date = "2020-01-01T01:02:03Z" +} +`, applicationId, keyId, value) +} diff --git a/azurerm/resource_arm_azuread_service_principal_test.go b/azurerm/resource_arm_azuread_service_principal_test.go new file mode 100644 index 000000000000..cea7ebf6624b --- /dev/null +++ b/azurerm/resource_arm_azuread_service_principal_test.go @@ -0,0 +1,91 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/google/uuid" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func TestAccAzureRMActiveDirectoryServicePrincipal_basic(t *testing.T) { + resourceName := "azurerm_azuread_service_principal.test" + id := uuid.New().String() + config := testAccAzureRMActiveDirectoryServicePrincipal_basic(id) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMActiveDirectoryServicePrincipalDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMActiveDirectoryServicePrincipalExists(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "display_name"), + resource.TestCheckResourceAttrSet(resourceName, "application_id"), + ), + }, + }, + }) +} + +func testCheckAzureRMActiveDirectoryServicePrincipalExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %q", name) + } + + client := testAccProvider.Meta().(*ArmClient).servicePrincipalsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + resp, err := client.Get(ctx, rs.Primary.ID) + + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Bad: Azure AD Service Principal %q does not exist", rs.Primary.ID) + } + return fmt.Errorf("Bad: Get on Azure AD servicePrincipalsClient: %+v", err) + } + + return nil + } +} + +func testCheckAzureRMActiveDirectoryServicePrincipalDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_azuread_service_principal" { + continue + } + + client := testAccProvider.Meta().(*ArmClient).servicePrincipalsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + resp, err := client.Get(ctx, rs.Primary.ID) + + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return nil + } + + return err + } + + return fmt.Errorf("Azure AD Service Principal still exists:\n%#v", resp) + } + + return nil +} + +func testAccAzureRMActiveDirectoryServicePrincipal_basic(id string) string { + return fmt.Sprintf(` +resource "azurerm_azuread_application" "test" { + name = "acctestspa%s" +} + +resource "azurerm_azuread_service_principal" "test" { + application_id = "${azurerm_azuread_application.test.application_id}" +} +`, id) +} diff --git a/website/azurerm.erb b/website/azurerm.erb index 313b5d32abc2..3c6a93370c37 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -27,10 +27,6 @@