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)
12672정성태6/15/202110598오류 유형: 725. IntelliJ에서 Java webapp 실행 시 "Address localhost:1099 is already in use" 오류
12671정성태6/15/202117270오류 유형: 724. Tomcat 실행 시 Failed to initialize connector [Connector[HTTP/1.1-8080]] 오류
12670정성태6/13/20218866.NET Framework: 1071. DLL Surrogate를 이용한 Out-of-process COM 개체에서의 CoInitializeSecurity 문제파일 다운로드1
12669정성태6/11/20218851.NET Framework: 1070. 사용자 정의 GetHashCode 메서드 구현은 C# 9.0의 record 또는 리팩터링에 맡기세요.
12668정성태6/11/202110555.NET Framework: 1069. C# - DLL Surrogate를 이용한 Out-of-process COM 개체 제작파일 다운로드2
12667정성태6/10/20219176.NET Framework: 1068. COM+ 서버 응용 프로그램을 이용해 CoInitializeSecurity 제약 해결파일 다운로드1
12666정성태6/10/20217859.NET Framework: 1067. 별도 DLL에 포함된 타입을 STAThread Main 메서드에서 사용하는 경우 CoInitializeSecurity 자동 호출파일 다운로드1
12665정성태6/9/20219182.NET Framework: 1066. Wslhub.Sdk 사용으로 알아보는 CoInitializeSecurity 사용 제약파일 다운로드1
12664정성태6/9/20217492오류 유형: 723. COM+ PIA 참조 시 "This operation failed because the QueryInterface call on the COM component" 오류
12663정성태6/9/20218944.NET Framework: 1065. Windows Forms - 속성 창의 디자인 설정 지원: 문자열 목록 내에서 항목을 선택하는 TypeConverter 제작파일 다운로드1
12662정성태6/8/20218155.NET Framework: 1064. C# COM 개체를 PIA(Primary Interop Assembly)로써 "Embed Interop Types" 참조하는 방법파일 다운로드1
12661정성태6/4/202118718.NET Framework: 1063. C# - MQTT를 이용한 클라이언트/서버(Broker) 통신 예제 [4]파일 다운로드1
12660정성태6/3/20219820.NET Framework: 1062. Windows Forms - 폼 내에서 발생하는 마우스 이벤트를 자식 컨트롤 영역에 상관없이 수신하는 방법 [1]파일 다운로드1
12659정성태6/2/202111099Linux: 40. 우분투 설치 후 MBR 디스크 드라이브 여유 공간이 인식되지 않은 경우 - Logical Volume Management
12658정성태6/2/20218527Windows: 194. Microsoft Store에 있는 구글의 공식 Youtube App
12657정성태6/2/20219803Windows: 193. 윈도우 패키지 관리자 - winget 설치
12656정성태6/1/20218059.NET Framework: 1061. 서버 유형의 COM+에 적용할 수 없는 Server GC
12655정성태6/1/20217623오류 유형: 722. windbg/sos - savemodule - Fail to read memory
12654정성태5/31/20217616오류 유형: 721. Hyper-V - Saved 상태의 VM을 시작 시 오류 발생
12653정성태5/31/202110231.NET Framework: 1060. 닷넷 GC에 새롭게 구현되는 DPAD(Dynamic Promotion And Demotion for GC)
12652정성태5/31/20218358VS.NET IDE: 164. Visual Studio - Web Deploy로 Publish 시 암호창이 매번 뜨는 문제
12651정성태5/31/20218622오류 유형: 720. PostgreSQL - ERROR: 22P02: malformed array literal: "..."
12650정성태5/17/20217926기타: 82. OpenTabletDriver의 버튼에 더블 클릭을 매핑 및 게임에서의 지원 방법
12649정성태5/16/20219268.NET Framework: 1059. 세대 별 GC(Garbage Collection) 방식에서 Card table의 사용 의미 [1]
12648정성태5/16/20217901사물인터넷: 66. PC -> FTDI -> NodeMCU v1 ESP8266 기기를 UART 핀을 연결해 직렬 통신하는 방법파일 다운로드1
12647정성태5/15/20219133.NET Framework: 1058. C# - C++과의 연동을 위한 구조체의 fixed 배열 필드 사용파일 다운로드1
... 31  32  33  34  35  36  37  [38]  39  40  41  42  43  44  45  ...