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

Owned objects API for service principal #38

Merged
merged 1 commit into from
May 17, 2021
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
32 changes: 32 additions & 0 deletions msgraph/serviceprincipals.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,35 @@ func (c *ServicePrincipalsClient) RemovePassword(ctx context.Context, servicePri
}
return status, nil
}

// ListOwnedObjects retrieves the owned objects of the specified Service Principal.
// id is the object ID of the service principal.
func (c *ServicePrincipalsClient) ListOwnedObjects(ctx context.Context, id string) (*[]string, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/servicePrincipals/%s/ownedObjects", id),
Params: url.Values{"$select": []string{"id"}},
HasTenantId: true,
},
})
if err != nil {
return nil, status, err
}
defer resp.Body.Close()
respBody, _ := ioutil.ReadAll(resp.Body)
var data struct {
OwnedObjects []struct {
Type string `json:"@odata.type"`
Id string `json:"id"`
} `json:"value"`
}
if err := json.Unmarshal(respBody, &data); err != nil {
return nil, status, err
}
ret := make([]string, len(data.OwnedObjects))
for i, v := range data.OwnedObjects {
ret[i] = v.Id
}
return &ret, status, nil
}
101 changes: 101 additions & 0 deletions msgraph/serviceprincipals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ func TestServicePrincipalsClient(t *testing.T) {
AppId: app.AppId,
DisplayName: utils.StringPtr(fmt.Sprintf("test-serviceprincipal-%s", c.randomString)),
})

appChild := testApplicationsClient_Create(t, a, msgraph.Application{
DisplayName: utils.StringPtr(fmt.Sprintf("test-serviceprincipal-child%s", a.randomString)),
})
spChild := testServicePrincipalsClient_Create(t, c, msgraph.ServicePrincipal{
AccountEnabled: utils.BoolPtr(true),
AppId: appChild.AppId,
DisplayName: utils.StringPtr(fmt.Sprintf("test-serviceprincipal-child%s", c.randomString)),
})

spChild.AppendOwner(string(c.client.BaseClient.Endpoint), string(c.client.BaseClient.ApiVersion), *sp.ID)
testServicePrincipalsClient_AddOwners(t, c, spChild)
testServicePrincipalsClient_ListOwners(t, c, *spChild.ID, []string{*sp.ID})
testServicePrincipalsClient_GetOwner(t, c, *spChild.ID, *sp.ID)
testServicePrincipalsClient_Get(t, c, *sp.ID)
sp.Tags = &([]string{"TestTag"})
testServicePrincipalsClient_Update(t, c, *sp)
Expand Down Expand Up @@ -75,12 +89,17 @@ func TestServicePrincipalsClient(t *testing.T) {
testGroupsClient_AddMembers(t, g, groupChild)

testServicePrincipalsClient_ListGroupMemberships(t, c, *sp.ID)
testServicePrincipalsClient_ListOwnedObjects(t, c, *sp.ID)

testServicePrincipalsClient_RemoveOwners(t, c, *spChild.ID, []string{*sp.ID})
testGroupsClient_Delete(t, g, *groupParent.ID)
testGroupsClient_Delete(t, g, *groupChild.ID)

testServicePrincipalsClient_Delete(t, c, *sp.ID)
testServicePrincipalsClient_Delete(t, c, *spChild.ID)

testApplicationsClient_Delete(t, a, *app.ID)
testApplicationsClient_Delete(t, a, *appChild.ID)
}

func testServicePrincipalsClient_Create(t *testing.T, c ServicePrincipalsClientTest, sp msgraph.ServicePrincipal) (servicePrincipal *msgraph.ServicePrincipal) {
Expand Down Expand Up @@ -187,3 +206,85 @@ func testServicePrincipalsClient_RemovePassword(t *testing.T, c ServicePrincipal
t.Fatalf("ServicePrincipalsClient.RemovePassword(): invalid status: %d", status)
}
}

func testServicePrincipalsClient_AddOwners(t *testing.T, c ServicePrincipalsClientTest, sp *msgraph.ServicePrincipal) {
status, err := c.client.AddOwners(c.connection.Context, sp)
if err != nil {
t.Fatalf("ServicePrincipalsClient.AddOwners(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ServicePrincipalsClient.AddOwners(): invalid status: %d", status)
}
}

func testServicePrincipalsClient_ListOwnedObjects(t *testing.T, c ServicePrincipalsClientTest, id string) (ownedObjects *[]string) {
ownedObjects, _, err := c.client.ListOwnedObjects(c.connection.Context, id)
if err != nil {
t.Fatalf("ServicePrincipalsClient.ListOwnedObjects(): %v", err)
}

if ownedObjects == nil {
t.Fatal("ServicePrincipalsClient.ListOwnedObjects(): ownedObjects was nil")
}

if len(*ownedObjects) != 1 {
t.Fatalf("ServicePrincipalsClient.ListOwnedObjects(): expected ownedObjects length 1. was: %d", len(*ownedObjects))
}
return
}

func testServicePrincipalsClient_ListOwners(t *testing.T, c ServicePrincipalsClientTest, id string, expected []string) (owners *[]string) {
owners, status, err := c.client.ListOwners(c.connection.Context, id)
if err != nil {
t.Fatalf("ServicePrincipalsClient.ListOwners(): %v", err)
}

if status < 200 || status >= 300 {
t.Fatalf("ServicePrincipalsClient.ListOwners(): invalid status: %d", status)
}

ownersExpected := len(expected)

if len(*owners) < ownersExpected {
t.Fatalf("ServicePrincipalsClient.ListOwners(): expected at least %d owner. has: %d", ownersExpected, len(*owners))
}

var ownersFound int

for _, e := range expected {
for _, o := range *owners {
if e == o {
ownersFound++
continue
}
}
}

if ownersFound < ownersExpected {
t.Fatalf("ServicePrincipalsClient.ListOwners(): expected %d matching owners. found: %d", ownersExpected, ownersFound)
}
return
}

func testServicePrincipalsClient_GetOwner(t *testing.T, c ServicePrincipalsClientTest, spId, ownerId string) (owner *string) {
owner, status, err := c.client.GetOwner(c.connection.Context, spId, ownerId)
if err != nil {
t.Fatalf("ServicePrincipalsClient.GetOwner(): %v", err)
}

if status < 200 || status >= 300 {
t.Fatalf("ServicePrincipalsClient.GetOwner(): invalid status: %d", status)
}

if owner == nil {
t.Fatalf("ServicePrincipalsClient.GetOwner(): owner was nil")
}
return
}

func testServicePrincipalsClient_RemoveOwners(t *testing.T, c ServicePrincipalsClientTest, spId string, ownerIds []string) {
_, err := c.client.RemoveOwners(c.connection.Context, spId, &ownerIds)
if err != nil {
t.Fatalf("ServicePrincipalsClient.RemoveOwners(): %v", err)
}
}