func (s *InteractiveSuite) TestInteractiveRoleAssignmentAlreadyExists(c *gc.C) { var requests []*http.Request senders := azuretesting.Senders{ oauthConfigSender(), deviceCodeSender(), tokenSender(), tokenSender(), tokenSender(), currentUserSender(), createServicePrincipalSender(), roleDefinitionListSender(), roleAssignmentAlreadyExistsSender(), } _, _, err := azureauth.InteractiveCreateServicePrincipal( ioutil.Discard, &senders, azuretesting.RequestRecorder(&requests), "https://arm.invalid", "https://graph.invalid", "22222222-2222-2222-2222-222222222222", s.clock, s.newUUID, ) c.Assert(err, jc.ErrorIsNil) }
func (s *InteractiveSuite) TestInteractiveServicePrincipalAlreadyExists(c *gc.C) { var requests []*http.Request senders := azuretesting.Senders{ oauthConfigSender(), deviceCodeSender(), tokenSender(), tokenSender(), tokenSender(), currentUserSender(), createServicePrincipalAlreadyExistsSender(), servicePrincipalListSender(), passwordCredentialsListSender(), updatePasswordCredentialsSender(), roleDefinitionListSender(), roleAssignmentAlreadyExistsSender(), } _, password, err := azureauth.InteractiveCreateServicePrincipal( ioutil.Discard, &senders, azuretesting.RequestRecorder(&requests), "https://arm.invalid", "https://graph.invalid", "22222222-2222-2222-2222-222222222222", s.clock, s.newUUID, ) c.Assert(err, jc.ErrorIsNil) c.Assert(password, gc.Equals, "33333333-3333-3333-3333-333333333333") c.Assert(requests, gc.HasLen, 10) c.Check(requests[0].URL.Path, gc.Equals, "/subscriptions/22222222-2222-2222-2222-222222222222") c.Check(requests[1].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/oauth2/devicecode") c.Check(requests[2].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/oauth2/token") c.Check(requests[3].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/me") c.Check(requests[4].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/servicePrincipals") // create c.Check(requests[5].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/servicePrincipals") // list c.Check(requests[6].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/servicePrincipals/sp-object-id/passwordCredentials") // list c.Check(requests[7].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/servicePrincipals/sp-object-id/passwordCredentials") // update c.Check(requests[8].URL.Path, gc.Equals, "/subscriptions/22222222-2222-2222-2222-222222222222/providers/Microsoft.Authorization/roleDefinitions") c.Check(requests[9].URL.Path, gc.Equals, "/subscriptions/22222222-2222-2222-2222-222222222222/providers/Microsoft.Authorization/roleAssignments/55555555-5555-5555-5555-555555555555") // Make sure that we don't wipe existing password credentials, and that // the new password credential matches the one returned from the // function. var params ad.PasswordCredentialsUpdateParameters err = json.NewDecoder(requests[7].Body).Decode(¶ms) c.Assert(err, jc.ErrorIsNil) c.Assert(params.Value, gc.HasLen, 2) c.Assert(params.Value[0], jc.DeepEquals, ad.PasswordCredential{ KeyId: "password-credential-key-id", StartDate: time.Time{}.UTC(), EndDate: time.Time{}.UTC(), }) assertPasswordCredential(c, params.Value[1]) }
func (s *InteractiveSuite) TestInteractive(c *gc.C) { var requests []*http.Request senders := azuretesting.Senders{ oauthConfigSender(), deviceCodeSender(), tokenSender(), // CheckForUserCompletion returns a token. // Token.Refresh returns a token. We do this // twice: once for ARM, and once for AAD. tokenSender(), tokenSender(), currentUserSender(), createServicePrincipalSender(), roleDefinitionListSender(), roleAssignmentSender(), } var stderr bytes.Buffer subscriptionId := "22222222-2222-2222-2222-222222222222" appId, password, err := azureauth.InteractiveCreateServicePrincipal( &stderr, &senders, azuretesting.RequestRecorder(&requests), "https://arm.invalid", "https://graph.invalid", subscriptionId, s.clock, s.newUUID, ) c.Assert(err, jc.ErrorIsNil) c.Assert(appId, gc.Equals, "cbb548f1-5039-4836-af0b-727e8571f6a9") c.Assert(password, gc.Equals, "33333333-3333-3333-3333-333333333333") c.Assert(stderr.String(), gc.Equals, ` Initiating interactive authentication. open your browser, etc. Authenticated as "Foo Bar". Creating/updating service principal. Assigning Owner role to service principal. `[1:]) // Token refreshes don't go through the inspectors. c.Assert(requests, gc.HasLen, 7) c.Check(requests[0].URL.Path, gc.Equals, "/subscriptions/22222222-2222-2222-2222-222222222222") c.Check(requests[1].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/oauth2/devicecode") c.Check(requests[2].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/oauth2/token") c.Check(requests[3].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/me") c.Check(requests[4].URL.Path, gc.Equals, "/11111111-1111-1111-1111-111111111111/servicePrincipals") c.Check(requests[5].URL.Path, gc.Equals, "/subscriptions/22222222-2222-2222-2222-222222222222/providers/Microsoft.Authorization/roleDefinitions") c.Check(requests[6].URL.Path, gc.Equals, "/subscriptions/22222222-2222-2222-2222-222222222222/providers/Microsoft.Authorization/roleAssignments/55555555-5555-5555-5555-555555555555") // The service principal creation includes the password. Check that the // password returned from the function is the same as the one set in the // request. var params ad.ServicePrincipalCreateParameters err = json.NewDecoder(requests[4].Body).Decode(¶ms) c.Assert(err, jc.ErrorIsNil) c.Assert(params.PasswordCredentials, gc.HasLen, 1) assertPasswordCredential(c, params.PasswordCredentials[0]) }