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)
11844정성태3/14/201915111개발 환경 구성: 434. Visual Studio 2019 - 리눅스 프로젝트를 이용한 공유/실행(so/out) 프로그램 개발 환경 설정 [1]파일 다운로드1
11843정성태3/14/201910856기타: 75. MSDN 웹 사이트를 기본으로 영문 페이지로 열고 싶다면?
11842정성태3/13/201910267개발 환경 구성: 433. 마이크로소프트의 CoreCLR 프로파일러 예제를 Visual Studio CMake로 빌드하는 방법 [1]파일 다운로드1
11841정성태3/13/201910229VS.NET IDE: 132. Visual Studio 2019 - CMake의 컴파일러를 기본 g++에서 clang++로 변경
11840정성태3/13/201911335오류 유형: 526. 윈도우 10 Ubuntu App 환경에서는 USB 외장 하드 접근 불가
11839정성태3/12/201914105디버깅 기술: 124. .NET Core 웹 앱을 호스팅하는 Azure App Services의 프로세스 메모리 덤프 및 windbg 분석 개요 [3]
11838정성태3/7/201916851.NET Framework: 811. (번역글) .NET Internals Cookbook Part 1 - Exceptions, filters and corrupted processes [1]파일 다운로드1
11837정성태3/6/201926567기타: 74. 도서: 시작하세요! C# 7.3 프로그래밍 [10]
11836정성태3/5/201914408오류 유형: 525. Visual Studio 2019 Preview 4/RC - C# 8.0 Missing compiler required member 'System.Range..ctor' [1]
11835정성태3/5/201914180.NET Framework: 810. C# 8.0의 Index/Range 연산자를 .NET Framework에서 사용하는 방법 및 비동기 스트림의 컴파일 방법 [3]파일 다운로드1
11834정성태3/4/201913070개발 환경 구성: 432. Visual Studio 없이 최신 C# (8.0) 컴파일러를 사용하는 방법
11833정성태3/4/201913833개발 환경 구성: 431. Visual Studio 2019 - CMake를 이용한 공유/실행(so/out) 리눅스 프로젝트 설정파일 다운로드1
11832정성태3/4/201910853오류 유형: 524. Visual Studio CMake - rsync: connection unexpectedly closed
11831정성태3/4/201910452오류 유형: 523. Visual Studio 2019 - 새 창으로 뜬 윈도우를 닫을 때 비정상 종료
11830정성태2/26/201910243오류 유형: 522. 이벤트 로그 - Error opening event log file State. Log will not be processed. Return code from OpenEventLog is 87.
11829정성태2/26/201912142개발 환경 구성: 430. 마이크로소프트의 CoreCLR 프로파일러 예제 빌드 방법 - 리눅스 환경 [1]
11828정성태2/26/201918549개발 환경 구성: 429. Component Services 관리자의 RuntimeBroker 설정이 2개 있는 경우 [8]
11827정성태2/26/201912455오류 유형: 521. Visual Studio - Could not start the 'rsync' command on the remote host, please install it using your system package manager.
11826정성태2/26/201912341오류 유형: 520. 우분투에 .NET Core SDK 설치 시 패키지 의존성 오류
11825정성태2/25/201917204개발 환경 구성: 428. Visual Studio 2019 - CMake를 이용한 리눅스 빌드 환경 설정 [1]
11824정성태2/25/201912000오류 유형: 519. The SNMP Service encountered an error while accessing the registry key SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration. [1]
11823정성태2/21/201913498오류 유형: 518. IIS 관리 콘솔이 뜨지 않는 문제
11822정성태2/20/201911632오류 유형: 517. docker에 설치한 MongoDB 서버로 연결이 안 되는 경우
11821정성태2/20/201912085오류 유형: 516. Visual Studio 2019 - This extension uses deprecated APIs and is at risk of not functioning in a future VS update. [1]
11820정성태2/20/201915229오류 유형: 515. 윈도우 10 1809 업데이트 후 "User Profiles Service" 1534 경고 발생
11819정성태2/20/201913901Windows: 158. 컴퓨터와 사용자의 SID(security identifier) 확인 방법
... 61  62  63  64  65  66  67  68  69  70  71  [72]  73  74  75  ...