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

비밀번호

댓글 작성자
 




... [61]  62  63  64  65  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12107정성태1/10/202010694.NET Framework: 877. C# - 프로세스의 모든 핸들을 열람 - 두 번째 이야기
12106정성태1/8/202012121VC++: 136. C++ - OSR Driver Loader와 같은 Legacy 커널 드라이버 설치 프로그램 제작 [1]
12105정성태1/8/202010761디버깅 기술: 153. C# - PEB를 조작해 로드된 DLL을 숨기는 방법
12104정성태1/7/202011466DDK: 9. 커널 메모리를 읽고 쓰는 NT Legacy driver와 C# 클라이언트 프로그램 [4]
12103정성태1/7/202014162DDK: 8. Visual Studio 2019 + WDK Legacy Driver 제작- Hello World 예제 [1]파일 다운로드2
12102정성태1/6/202011808디버깅 기술: 152. User 권한(Ring 3)의 프로그램에서 _ETHREAD 주소(및 커널 메모리를 읽을 수 있다면 _EPROCESS 주소) 구하는 방법
12101정성태1/5/202011142.NET Framework: 876. C# - PEB(Process Environment Block)를 통해 로드된 모듈 목록 열람
12100정성태1/3/20209161.NET Framework: 875. .NET 3.5 이하에서 IntPtr.Add 사용
12099정성태1/3/202011462디버깅 기술: 151. Windows 10 - Process Explorer로 확인한 Handle 정보를 windbg에서 조회 [1]
12098정성태1/2/202011056.NET Framework: 874. C# - 커널 구조체의 Offset 값을 하드 코딩하지 않고 사용하는 방법 [3]
12097정성태1/2/20209623디버깅 기술: 150. windbg - Wow64, x86, x64에서의 커널 구조체(예: TEB) 구조체 확인
12096정성태12/30/201911629디버깅 기술: 149. C# - DbgEng.dll을 이용한 간단한 디버거 제작 [1]
12095정성태12/27/201913038VC++: 135. C++ - string_view의 동작 방식
12094정성태12/26/201911180.NET Framework: 873. C# - 코드를 통해 PDB 심벌 파일 다운로드 방법
12093정성태12/26/201911191.NET Framework: 872. C# - 로딩된 Native DLL의 export 함수 목록 출력파일 다운로드1
12092정성태12/25/201910608디버깅 기술: 148. cdb.exe를 이용해 (ntdll.dll 등에 정의된) 커널 구조체 출력하는 방법
12091정성태12/25/201912120디버깅 기술: 147. pdb 파일을 다운로드하기 위한 symchk.exe 실행에 필요한 최소 파일 [1]
12090정성태12/24/201910812.NET Framework: 871. .NET AnyCPU로 빌드된 PE 헤더의 로딩 전/후 차이점 [1]파일 다운로드1
12089정성태12/23/201911509디버깅 기술: 146. gflags와 _CrtIsMemoryBlock을 이용한 Heap 메모리 손상 여부 체크
12088정성태12/23/201910493Linux: 28. Linux - 윈도우의 "Run as different user" 기능을 shell에서 실행하는 방법
12087정성태12/21/201910920디버깅 기술: 145. windbg/sos - Dictionary의 entries 배열 내용을 모두 덤프하는 방법 (do_hashtable.py) [1]
12086정성태12/20/201912992디버깅 기술: 144. windbg - Marshal.FreeHGlobal에서 발생한 덤프 분석 사례
12085정성태12/20/201910723오류 유형: 586. iisreset - The data is invalid. (2147942413, 8007000d) 오류 발생 - 두 번째 이야기 [1]
12084정성태12/19/201911373디버깅 기술: 143. windbg/sos - Hashtable의 buckets 배열 내용을 모두 덤프하는 방법 (do_hashtable.py) [1]
12083정성태12/17/201912619Linux: 27. linux - lldb를 이용한 .NET Core 응용 프로그램의 메모리 덤프 분석 방법 [2]
12082정성태12/17/201912471오류 유형: 585. lsof: WARNING: can't stat() fuse.gvfsd-fuse file system
... [61]  62  63  64  65  66  67  68  69  70  71  72  73  74  75  ...