Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 5개 있습니다.)
.NET Framework: 735. Azure - PowerShell로 Access control(IAM)에 새로운 계정 만드는 방법
; https://www.sysnet.pe.kr/2/0/11479

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

개발 환경 구성: 367. Azure - New-AzureRmADServicePrincipal / New-AzureRmRoleAssignment 명령어
; https://www.sysnet.pe.kr/2/0/11499

개발 환경 구성: 368. PowerShell로 접근하는 Azure의 Access control 보안과 Azure Active Directory의 계정 관리 서비스
; https://www.sysnet.pe.kr/2/0/11500

개발 환경 구성: 369. New-AzureRmADServicePrincipal로 생성한 계정의 clientSecret, key 값을 구하는 방법
; https://www.sysnet.pe.kr/2/0/11501




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

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  8  [9]  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13718정성태8/27/20247417오류 유형: 921. Visual C++ - error C1083: Cannot open include file: 'float.h': No such file or directory [2]
13717정성태8/26/20247022VS.NET IDE: 192. Visual Studio 2022 - Windows XP / 2003용 C/C++ 프로젝트 빌드
13716정성태8/21/20246757C/C++: 167. Visual C++ - 윈도우 환경에서 _execv 동작 [1]
13715정성태8/19/20247361Linux: 78. 리눅스 C/C++ - 특정 버전의 glibc 빌드 (docker-glibc-builder)
13714정성태8/19/20246750닷넷: 2295. C# 12 - 기본 생성자(Primary constructors) (책 오타 수정) [3]
13713정성태8/16/20247455개발 환경 구성: 721. WSL 2에서의 Hyper-V Socket 연동
13712정성태8/14/20247219개발 환경 구성: 720. Synology NAS - docker 원격 제어를 위한 TCP 바인딩 추가
13711정성태8/13/20248062Linux: 77. C# / Linux - zombie process (defunct process) [1]파일 다운로드1
13710정성태8/8/20247980닷넷: 2294. C# 13 - (6) iterator 또는 비동기 메서드에서 ref와 unsafe 사용을 부분적으로 허용파일 다운로드1
13709정성태8/7/20247751닷넷: 2293. C# - safe/unsafe 문맥에 대한 C# 13의 (하위 호환을 깨는) 변화파일 다운로드1
13708정성태8/7/20247531개발 환경 구성: 719. ffmpeg / YoutubeExplode - mp4 동영상 파일로부터 Audio 파일 추출
13707정성태8/6/20247771닷넷: 2292. C# - 자식 프로세스의 출력이 4,096보다 많은 경우 Process.WaitForExit 호출 시 hang 현상파일 다운로드1
13706정성태8/5/20247881개발 환경 구성: 718. Hyper-V - 리눅스 VM에 새로운 디스크 추가
13705정성태8/4/20248153닷넷: 2291. C# 13 - (5) params 인자 타입으로 컬렉션 허용 [2]파일 다운로드1
13704정성태8/2/20248110닷넷: 2290. C# - 간이 dotnet-dump 프로그램 만들기파일 다운로드1
13703정성태8/1/20247437닷넷: 2289. "dotnet-dump ps" 명령어가 닷넷 프로세스를 찾는 방법
13702정성태7/31/20247840닷넷: 2288. Collection 식을 지원하는 사용자 정의 타입을 CollectionBuilder 특성으로 성능 보완파일 다운로드1
13701정성태7/30/20248109닷넷: 2287. C# 13 - (4) Indexer를 이용한 개체 초기화 구문에서 System.Index 연산자 허용파일 다운로드1
13700정성태7/29/20247714디버깅 기술: 200. DLL Export/Import의 Hint 의미
13699정성태7/27/20248237닷넷: 2286. C# 13 - (3) Monitor를 대체할 Lock 타입파일 다운로드1
13698정성태7/27/20248194닷넷: 2285. C# - async 메서드에서의 System.Threading.Lock 잠금 처리파일 다운로드1
13697정성태7/26/20247920닷넷: 2284. C# - async 메서드에서의 lock/Monitor.Enter/Exit 잠금 처리파일 다운로드1
13696정성태7/26/20247454오류 유형: 920. dotnet publish - error NETSDK1047: Assets file '...\obj\project.assets.json' doesn't have a target for '...'
13695정성태7/25/20247438닷넷: 2283. C# - Lock / Wait 상태에서도 STA COM 메서드 호출 처리파일 다운로드1
13694정성태7/25/20247905닷넷: 2282. C# - ASP.NET Core Web App의 Request 용량 상한값 (Kestrel, IIS)
13693정성태7/24/20247233개발 환경 구성: 717. Visual Studio - C# 프로젝트에서 레지스트리에 등록하지 않은 COM 개체 참조 및 사용 방법파일 다운로드1
1  2  3  4  5  6  7  8  [9]  10  11  12  13  14  15  ...