Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)

PowerShell로 접근하는 Azure의 Access control 보안과 Azure Active Directory의 계정 관리 서비스

지난 글에서,

Azure의 Access control 보안과 Azure Active Directory의 계정 관리 서비스
; https://www.sysnet.pe.kr/2/0/11495

Azure의 Access Control과 Active Directory 관리의 차이점을 알아봤고, 아래의 글을 통해 Azure Portal에서 Access Control의 독립적인 권한 부여 방법을 화면을 통해 알아봤습니다.

Azure 리소스의 액세스 제어(Access control)별로 사용자에게 권한을 할당하는 방법
; https://www.sysnet.pe.kr/2/0/11497

그리고 또 다른 글에서 AAD와 Access Control에 PowerShell을 이용해 계정을 생성해 본 적이 있습니다.

Azure - PowerShell로 Access control(IAM)에 새로운 계정 만드는 방법
; https://www.sysnet.pe.kr/2/0/11479

Azure - New-AzureRmADServicePrincipal / New-AzureRmRoleAssignment 명령어
; https://www.sysnet.pe.kr/2/0/11499

위의 글에 소개했던 스크립트로,

PS C:\Windows\System32> Login-AzureRmAccount

Account          : test@testad.com
SubscriptionName : MySub
SubscriptionId   : 483be72d-ad39-426f-a375-4872acf06d0d
TenantId         : ddafe5fd-4006-452d-a6c1-7d0d8df6509d
Environment      : AzureCloud

PS C:\Windows\System32> $plainText = "testpassword"
PS C:\Windows\System32> $secureString = ConvertTo-SecureString $plainText -AsPlainText -Force
PS C:\Windows\System32> $sp = New-AzureRmADServicePrincipal -DisplayName "NewUser1" -Password $secureString
PS C:\Windows\System32> New-AzureRmRoleAssignment -ServicePrincipalName $sp.ApplicationId -RoleDefinitionName Contributor

생성한 계정의 권한 부여는 Azure 리소스 구독의 최상단 루트 레벨에 속한다고 했습니다.




그런데, New-AzureRmADServicePrincipal/New-AzureRmRoleAssignment 스크립트가 권한 문제로 실패하는 경우가 있습니다. "Azure의 Access control 보안과 Azure Active Directory의 계정 관리 서비스" 글에서 설명한 데로 MySub 구독에 대해 다음의 사용자들이 권한이 있었는데,

test@testad.com

    * 마이크로소프트 계정
    * Azure 사이트 로그인 계정
    * MySub 구독의 Owner 계정
    * MySub - Azure Active Directory의 Global administrator Role에 속한 Member

user@my.com

    * 마이크로소프트 계정
    * Azure 사이트 로그인 계정
    * MySub 구독의 Contributor 계정
    * MySub - Azure Active Directory의 Guest

위와 같은 상황에서 user@my.com 권한의 사용자가 New-AzureRmADServicePrincipal 명령을 수행하는 경우 다음과 같은 보안 오류가 떨어집니다.

PS C:\WINDOWS\system32> $sp = New-AzureRmADServicePrincipal -DisplayName "NewUser1" -Password $secureString
New-AzureRmADServicePrincipal : You are a guest user in the directory and are not allowed to create an application. Please contact the administrator of the directory.
At line:1 char:7
+ $sp = New-AzureRmADServicePrincipal -DisplayName "NewUser1" -Password ...
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureRmADServicePrincipal], InvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ActiveDirectory.NewAzureADServicePrincipalCommand

왜냐하면 user@my.com은 MySub 구독의 AAD에 Guest 사용자로 등록되어 있기 때문에 신규 사용자를 생성할 수 있는 권한이 없으므로 예외가 발생합니다. 따라서 AAD에서 user@my.com 계정을 "Limited administrator"로 바꾼 후 "Security administrator" Role을 부여해야 합니다.

하지만 AAD에서 "Security administrator" 권한인데도 이번에는 New-AzureRmRoleAssignment 단계에서 다음과 같은 오류가 발생할 수 있습니다.

PS C:\Windows\System32> New-AzureRmRoleAssignment -ServicePrincipalName $sp.ApplicationId -RoleDefinitionName Contributor
New-AzureRmRoleAssignment : The client 'user@my.com' with object id 'd9f2e68f-9e12-4d91-909a-10920667a2ca' does not have authorization to perform action 'Microsoft.Authorization/roleAssignments/write' over scope '/subscriptions/483be72d-ad39-426f-a375-4872acf06d0d'.
At line:1 char:1
+ New-AzureRmRoleAssignment -ServicePrincipalName $sp.ApplicationId -Ro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureRmRoleAssignment], CloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand

이유는 간단합니다. MySub 구독에 대해 Contributor 권한만을 가진 user@my.com 계정이 새로운 사용자 권한을 부여하려 했기 때문에 "does not have authorization" 오류가 발생하는 것입니다.

Contributor는 Azure 구독에 대해 계정 권한을 변경할 수 있는 권한이 없고, 이것이 가능하려면 "User Access Administrator(사용자 액세스 관리자)" Role을 user@my.com 사용자에게 추가해야 합니다. 다음의 글이 이 방법을 설명한 것입니다.

Azure 리소스의 액세스 제어(Access control)별로 사용자에게 권한을 할당하는 방법
; https://www.sysnet.pe.kr/2/0/11497

정리하면, New-AzureRmADServicePrincipal의 경우 AAD에 "Security administrator" role이 있어야 하고, New-AzureRmRoleAssignment의 경우 Access control에서 "User Access Administrator(사용자 액세스 관리자)" role이 있어야 합니다.




그렇다면, 프로그램으로 특정 Azure 리소스 위치의 액세스 제어에 사용자 권한을 추가하는 것은 어떻게 할 수 있을까요? 이미 살펴봤던 New-AzureRmRoleAssignment 명령어를 보면,

New-AzureRmRoleAssignment -ServicePrincipalName $sp.ApplicationId -RoleDefinitionName Contributor

리소스의 위치를 특정하는 옵션 없이 실행했으므로 "Azure 구독" 레벨에서 생성을 시도하게 됩니다. 물론 Azure 구독 레벨에 role을 부여하면 되겠지만 그럼 너무 큰 권한이 주어지는 것입니다. 단지, 특정 사용자에게 vm1 가상 머신에 대해서만 Contributor 권한을 주고 싶은 경우가 있다면 어떻게 할 수 있느냐 하는 것입니다.

이를 위해 다음과 같이 ResourceGroupName, ResourceType, ResourceName 옵션으로 리소스의 위치를 지정하면 됩니다.

New-AzureRmRoleAssignment -ResourceGroupName TestGrp -ResourceType Microsoft.Compute/virtualMachines -ResourceName vm1 -ServicePrincipalName $sp.ApplicationId -RoleDefinitionName Contributor

RoleAssignmentId   : /subscriptions/461e7265-0395-49b1-94b3-4c891430a893/resourceGroups/TestGrp/providers/Microsoft.Compute/virtualMachines/vm1
                     /providers/Microsoft.Authorization/roleAssignments/9cf4aeb4-f5ca-4f90-9d39-c63b324f411a
Scope              : /subscriptions/461e7265-0395-49b1-94b3-4c891430a893/resourceGroups/TestGrp/providers/Microsoft.Compute/virtualMachines/vm1
DisplayName        : NewUser1
SignInName         : 
RoleDefinitionName : Contributor
RoleDefinitionId   : b24988ac-6180-42a0-ab88-20f7382dd24c
ObjectId           : 7a562971-4b67-4021-a3f0-8b7e5b586c4b
ObjectType         : ServicePrincipal
CanDelegate        : False

인자 값 중에 ResourceGroupName과 ResourceName은 Azure Portal에서도 쉽게 구할 수 있습니다. 어려운 것은 ResourceType인데요, 역시 Azure Portal에서 해당 리소스로 들어가 "Properties(속성)" 메뉴를 통해 들어가면 아래와 같이 "Resource ID" 값 중에 ResourceType 문자열이 들어있는 것을 확인할 수 있습니다.

program_auth_1.png

위의 예에서는 리소스 그룹에 속한 가상 머신 자원에 대해 지정했지만 나머지 리소스에 대해서도 유사한 방식으로 위치를 지정해 권한 부여를 하면 됩니다.




Login-AzureRmAccount 명령어 시 다음과 같은 오류가 발생할 수 있습니다.

PS C:\WINDOWS\system32> Login-AzureRmAccount
Login-AzureRmAccount : The term 'Login-AzureRmAccount' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Login-AzureRmAccount
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Login-AzureRmAccount:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

다음의 링크에 방문해 msi 파일을 설치해 주시면 됩니다.

Azure PowerShell 5.7.0
; https://github.com/Azure/azure-powershell/releases
; https://github.com/Azure/azure-powershell/releases/download/v5.7.0-April2018/azure-powershell.5.7.0.msi




하나의 계정에 다중 구독이 설정된 경우, Login-AzureRmAccount로 로그인하면 다음과 같은 식으로 현재 계정과 연결된 기본 구독 정보만 출력됩니다.

PS> Login-AzureRmAccount

Account          : testuser@hotmail.com
SubscriptionName : 무료 체험
SubscriptionId   : 483be72d-ad39-426f-a375-4872acf06d0d
TenantId         : ddafe5fd-4006-452d-a6c1-7d0d8df6509d
Environment      : AzureCloud

즉, 다른 구독 ID(SubscriptionId)가 안 보이는데요, 이럴 때 그냥 Get-AzureRmSubscription 명령어를 내리면 다음과 같이 현재 로그인한 사용자의 모든 구독을 출력해 줍니다.

PS> Get-AzureRmSubscription

Name     : 무료 체험
Id       : 483be72d-ad39-426f-a375-4872acf06d0d
TenantId : ddafe5fd-4006-452d-a6c1-7d0d8df6509d
State    : Enabled

Name     : Pay-As-You-Go
Id       : 439d30fb-81a3-44d2-9275-4fd93d325c49
TenantId : 909964c6-02ee-4ff8-a802-07f81ffe4838
State    : Enabled



하지만 이후 사용되는 모든 Azure 명령어는 기본적으로 Login-AzureRmAccount의 출력으로 나온 구독을 대상으로 합니다. 이를 변경하고 싶다면 Select-AzureRmSubscription 명령을 사용해야 하는데, 위의 경우 "Pay-As-You-Go" 구독으로 문맥을 변경하고 싶다면 다음과 같이 명령을 내릴 수 있습니다.

Select-AzureRmSubscription -SubscriptionId 439d30fb-81a3-44d2-9275-4fd93d325c49




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 4/17/2018]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 31  32  33  34  [35]  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12746정성태7/31/20218777개발 환경 구성: 588. 네트워크 장비 환경을 시뮬레이션하는 Packet Tracer 프로그램 소개
12745정성태7/31/20216628개발 환경 구성: 587. Azure Active Directory - tenant의 관리자 계정 로그인 방법
12744정성태7/30/20217236개발 환경 구성: 586. Azure Active Directory에 연결된 App 목록을 확인하는 방법?
12743정성태7/30/20217924.NET Framework: 1083. Azure Active Directory - 외부 Token Cache 저장소를 사용하는 방법파일 다운로드1
12742정성태7/30/20217221개발 환경 구성: 585. Azure AD 인증을 위한 사용자 인증 유형
12741정성태7/29/20218375.NET Framework: 1082. Azure Active Directory - Microsoft Graph API 호출 방법파일 다운로드1
12740정성태7/29/20217063오류 유형: 747. SharePoint - InvalidOperationException 0x80131509
12739정성태7/28/20217031오류 유형: 746. Azure Active Directory - IDW10106: The 'ClientId' option must be provided.
12738정성태7/28/20217600오류 유형: 745. Azure Active Directory - Client credential flows must have a scope value with /.default suffixed to the resource identifier (application ID URI).
12737정성태7/28/20216578오류 유형: 744. Azure Active Directory - The resource principal named api://...[client_id]... was not found in the tenant
12736정성태7/28/20217053오류 유형: 743. Active Azure Directory에서 "API permissions"의 권한 설정이 "Not granted for ..."로 나오는 문제
12735정성태7/27/20217573.NET Framework: 1081. C# - Azure AD 인증을 지원하는 데스크톱 애플리케이션 예제(Windows Forms) [2]파일 다운로드1
12734정성태7/26/202123543스크립트: 20. 특정 단어로 시작하거나/끝나는 문자열을 포함/제외하는 정규 표현식 - Look-around
12733정성태7/23/202110933.NET Framework: 1081. Self-Contained/SingleFile 유형의 .NET Core/5+ 실행 파일을 임베딩한다면? [1]파일 다운로드2
12732정성태7/23/20216232오류 유형: 742. SharePoint - The super user account utilized by the cache is not configured.
12731정성태7/23/20217338개발 환경 구성: 584. Add Internal URLs 화면에서 "Save" 버튼이 비활성화 된 경우
12730정성태7/23/20218877개발 환경 구성: 583. Visual Studio Code - Go 코드에서 입력을 받는 경우
12729정성태7/22/20217861.NET Framework: 1080. xUnit 단위 테스트에 메서드/클래스 수준의 문맥 제공 - Fixture
12728정성태7/22/20217343.NET Framework: 1079. MSTestv2 단위 테스트에 메서드/클래스/어셈블리 수준의 문맥 제공
12727정성태7/21/20218290.NET Framework: 1078. C# 단위 테스트 - MSTestv2/NUnit의 Assert.Inconclusive 사용법(?) [1]
12726정성태7/21/20218111VS.NET IDE: 169. 비주얼 스튜디오 - 단위 테스트 선택 시 MSTestv2 외의 xUnit, NUnit 사용법 [1]
12725정성태7/21/20216885오류 유형: 741. Failed to find the "go" binary in either GOROOT() or PATH
12724정성태7/21/20219530개발 환경 구성: 582. 윈도우 환경에서 Visual Studio Code + Go (Zip) 개발 환경 [1]
12723정성태7/21/20217172오류 유형: 740. SharePoint - Alternate access mappings have not been configured 경고
12722정성태7/20/20217025오류 유형: 739. MSVCR110.dll이 없어 exe 실행이 안 되는 경우
12721정성태7/20/20217643오류 유형: 738. The trust relationship between this workstation and the primary domain failed. - 세 번째 이야기
... 31  32  33  34  [35]  36  37  38  39  40  41  42  43  44  45  ...