-
Notifications
You must be signed in to change notification settings - Fork 33
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
Read-only Implementation of Managed Identities #933
Merged
Merged
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1034a9d
Bump version, changelog, and notice for release
nturinski 72df9ec
Read-only implementation of managed identities
nturinski 0aacc56
Merge from main
nturinski 8fede13
Save notice
nturinski e365bce
Fix package-lock.json
nturinski a582bbf
Reinit package-lock
nturinski dd53107
Clean up some role definitions code
nturinski 12b8874
Merge branch 'main' into nat/managedIdentityReadOnly
nturinski 775c644
Merge from main
nturinski 47dd35a
Merge branch 'nat/managedIdentityReadOnly' of https://github.com/micr…
nturinski d1936d6
PR feedback
nturinski 499ec6a
WIP
nturinski 85119a8
Merge from main
nturinski f0fd37f
Fix typings
nturinski a3cc389
Nest the target and source resources
nturinski 765f0d6
Merge branch 'main' into nat/managedIdentityReadOnly
nturinski a6ee1c9
Make role defintion id unique
nturinski 7d2a04a
Merge branch 'nat/managedIdentityReadOnly' of https://github.com/micr…
nturinski 07e06ac
Merge branch 'main' into nat/managedIdentityReadOnly
nturinski 0ed6e1b
Some cleanup/comments for clarity
nturinski 237549d
Merge branch 'nat/managedIdentityReadOnly' of https://github.com/micr…
nturinski fd22daf
Couple more clean-ups
nturinski aef9c22
Merge branch 'main' into nat/managedIdentityReadOnly
nturinski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { AuthorizationManagementClient, RoleAssignment } from '@azure/arm-authorization'; | ||
import { uiUtils } from '@microsoft/vscode-azext-azureutils'; | ||
import { callWithTelemetryAndErrorHandling, createSubscriptionContext, type IActionContext } from '@microsoft/vscode-azext-utils'; | ||
import { type AzureResource, type AzureResourceBranchDataProvider } from '@microsoft/vscode-azureresources-api'; | ||
import * as vscode from 'vscode'; | ||
import { localize } from 'vscode-nls'; | ||
import { ext } from '../extensionVariables'; | ||
import { ResourceGroupsItem } from '../tree/ResourceGroupsItem'; | ||
import { ManagedIdentityItem } from './ManagedIdentityItem'; | ||
export class ManagedIdentityBranchDataProvider extends vscode.Disposable implements AzureResourceBranchDataProvider<ResourceGroupsItem> { | ||
private readonly onDidChangeTreeDataEmitter = new vscode.EventEmitter<ResourceGroupsItem | undefined>(); | ||
public roleAssignmentsTask: Promise<{ [id: string]: RoleAssignment[] }>; | ||
|
||
constructor() { | ||
super( | ||
() => { | ||
this.onDidChangeTreeDataEmitter.dispose(); | ||
}); | ||
this.roleAssignmentsTask = this.initialize(); | ||
} | ||
|
||
get onDidChangeTreeData(): vscode.Event<ResourceGroupsItem | undefined> { | ||
return this.onDidChangeTreeDataEmitter.event; | ||
} | ||
|
||
public async initialize(): Promise<{ [id: string]: RoleAssignment[] }> { | ||
const provider = await ext.subscriptionProviderFactory(); | ||
const subscriptions = await provider.getSubscriptions(false); | ||
const roleAssignments: { [id: string]: RoleAssignment[] } = {}; | ||
await Promise.allSettled(subscriptions.map(async (subscription) => { | ||
const subContext = createSubscriptionContext(subscription); | ||
const authClient = new AuthorizationManagementClient(subContext.credentials, subContext.subscriptionId); | ||
roleAssignments[subscription.subscriptionId] = await uiUtils.listAllIterator(authClient.roleAssignments.listForSubscription()); | ||
})); | ||
|
||
return roleAssignments; | ||
} | ||
|
||
async getChildren(element: ResourceGroupsItem): Promise<ResourceGroupsItem[] | null | undefined> { | ||
return (await element.getChildren?.())?.map((child) => { | ||
if (child.id) { | ||
return ext.azureTreeState.wrapItemInStateHandling(child, () => this.refresh(child)) | ||
MicroFish91 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return child; | ||
}); | ||
} | ||
|
||
async getResourceItem(element: AzureResource): Promise<ResourceGroupsItem> { | ||
const resourceItem = await callWithTelemetryAndErrorHandling( | ||
'getResourceItem', | ||
async (context: IActionContext) => { | ||
context.errorHandling.rethrow = true; | ||
return new ManagedIdentityItem(element.subscription, element); | ||
}); | ||
|
||
if (!resourceItem) { | ||
throw new Error(localize('failedToGetResourceItem', 'Failed to get resource item for "{0}"', element.id)); | ||
} | ||
|
||
return ext.azureTreeState.wrapItemInStateHandling(resourceItem, () => this.refresh(resourceItem)); | ||
} | ||
|
||
async getTreeItem(element: ResourceGroupsItem): Promise<vscode.TreeItem> { | ||
return await element.getTreeItem(); | ||
} | ||
|
||
refresh(element?: ResourceGroupsItem): void { | ||
this.onDidChangeTreeDataEmitter.fire(element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { AuthorizationManagementClient } from "@azure/arm-authorization"; | ||
import { Identity, ManagedServiceIdentityClient } from "@azure/arm-msi"; | ||
import { uiUtils } from "@microsoft/vscode-azext-azureutils"; | ||
import { callWithTelemetryAndErrorHandling, createContextValue, createSubscriptionContext, nonNullProp, type IActionContext } from "@microsoft/vscode-azext-utils"; | ||
import { ResourceBase, type AzureResource, type AzureSubscription, type ViewPropertiesModel } from "@microsoft/vscode-azureresources-api"; | ||
import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from "vscode"; | ||
import { getAzExtResourceType } from "../../api/src/index"; | ||
import { getAzureResourcesService } from "../services/AzureResourcesService"; | ||
import { GenericItem } from "../tree/GenericItem"; | ||
import { getIconPath } from "../utils/azureUtils"; | ||
import { RoleAssignmentsItem } from "./RoleAssignmentsItem"; | ||
|
||
export class ManagedIdentityItem implements ResourceBase { | ||
static readonly contextValue: string = 'managedIdentityItem'; | ||
static readonly contextValueRegExp: RegExp = new RegExp(ManagedIdentityItem.contextValue); | ||
name: string; | ||
id: string; | ||
|
||
constructor(public readonly subscription: AzureSubscription, | ||
public readonly resource: AzureResource) { | ||
this.id = resource.id; | ||
this.name = resource.name; | ||
} | ||
|
||
viewProperties: ViewPropertiesModel = { | ||
data: this.resource, | ||
label: this.resource.name, | ||
} | ||
|
||
private get contextValue(): string { | ||
const values: string[] = []; | ||
values.push(ManagedIdentityItem.contextValue); | ||
return createContextValue(values); | ||
} | ||
|
||
async getChildren(): Promise<RoleAssignmentsItem[]> { | ||
const result = await callWithTelemetryAndErrorHandling('managedIdentityItem.getChildren', async (context: IActionContext) => { | ||
const subContext = createSubscriptionContext(this.subscription); | ||
const msiClient = new ManagedServiceIdentityClient(subContext.credentials, subContext.subscriptionId); | ||
const msi: Identity = await msiClient.userAssignedIdentities.get(nonNullProp(this.resource, 'resourceGroup'), this.resource.name); | ||
|
||
const resources = await getAzureResourcesService().listResources(context, this.subscription); | ||
|
||
const assignedRoleAssignment = new RoleAssignmentsItem('Assigned to', this.subscription, msi); | ||
const accessRoleAssignment = new RoleAssignmentsItem('Grants access to', this.subscription, msi); | ||
|
||
const assignedResources = resources.filter((r) => { | ||
const userAssignedIdentities = r.identity?.userAssignedIdentities; | ||
if (!userAssignedIdentities) { | ||
return false; | ||
} | ||
|
||
if (!msi.id) { | ||
return false; | ||
} | ||
|
||
return userAssignedIdentities[msi.id] !== undefined | ||
}).map((r) => { | ||
return new GenericItem(nonNullProp(r, 'name'), { iconPath: getIconPath(r.type ? getAzExtResourceType({ type: r.type }) : undefined) }); | ||
}); | ||
|
||
const authClient = new AuthorizationManagementClient(subContext.credentials, subContext.subscriptionId); | ||
|
||
const roleAssignment = await uiUtils.listAllIterator(authClient.roleAssignments.listForSubscription()); | ||
const filteredBySub = roleAssignment.filter((ra) => ra.principalId === msi.principalId); | ||
|
||
const targetResources = await accessRoleAssignment.getRoleDefinitionsItems(filteredBySub); | ||
assignedRoleAssignment.addChildren(assignedResources); | ||
accessRoleAssignment.addChildren(targetResources); | ||
accessRoleAssignment.addChild(new GenericItem('Show resources from other subscriptions...', | ||
{ | ||
iconPath: new ThemeIcon('sync'), | ||
commandId: 'azureResources.loadAllSubscriptionRoleAssignments', | ||
commandArgs: [accessRoleAssignment] | ||
})) | ||
|
||
const children = []; | ||
|
||
if ((await assignedRoleAssignment.getChildren()).length > 0) { | ||
// if there weren't any assigned resources, don't show that section | ||
children.push(assignedRoleAssignment); | ||
} | ||
|
||
children.push(accessRoleAssignment); | ||
return children; | ||
}); | ||
|
||
return result ?? []; | ||
} | ||
|
||
getTreeItem(): TreeItem { | ||
return { | ||
label: this.resource.name, | ||
id: this.id, | ||
iconPath: getIconPath(this.resource.resourceType), | ||
contextValue: this.contextValue, | ||
collapsibleState: TreeItemCollapsibleState.Collapsed, | ||
} | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What changed with the icon?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am honestly not entirely sure. I think at one point, I changed the name but I ended up reverting that. 🤔