This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
237 lines (195 loc) · 10.4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.Graph.RBAC.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
using System;
using System.IO;
using System.Linq;
namespace ManageServicePrincipalCredentials
{
public class Program
{
/**
* Azure Service Principal sample for managing Service Principal -
* - Create an Active Directory application
* - Create a Service Principal for the application and assign a role
* - Export the Service Principal to an authentication file
* - Use the file to list subcription virtual machines
* - Update the application
* - Delete the application and Service Principal.
*/
public static void RunSample(Azure.IAuthenticated authenticated, AzureEnvironment environment)
{
string spName = Utilities.CreateRandomName("sp");
string appName = SdkContext.RandomResourceName("app", 20);
string appUrl = "https://" + appName;
string passwordName1 = SdkContext.RandomResourceName("password", 20);
string password1 = "P@ssw0rd";
string passwordName2 = SdkContext.RandomResourceName("password", 20);
string password2 = "StrongP@ss!12";
string certName1 = SdkContext.RandomResourceName("cert", 20);
string raName = SdkContext.RandomGuid();
string servicePrincipalId = "";
try
{
// ============================================================
// Create application
Utilities.Log("Creating a service principal " + spName + "...");
IServicePrincipal servicePrincipal = authenticated.ServicePrincipals
.Define(appName)
.WithNewApplication(appUrl)
.DefinePasswordCredential(passwordName1)
.WithPasswordValue(password1)
.Attach()
.DefinePasswordCredential(passwordName2)
.WithPasswordValue(password2)
.Attach()
.DefineCertificateCredential(certName1)
.WithAsymmetricX509Certificate()
.WithPublicKey(File.ReadAllBytes(Path.Combine(Utilities.ProjectPath, "Asset", "NetworkTestCertificate1.cer")))
.WithDuration(TimeSpan.FromDays(1))
.Attach()
.Create();
Utilities.Log("Created service principal " + spName + ".");
Utilities.Print(servicePrincipal);
servicePrincipalId = servicePrincipal.Id;
// ============================================================
// Create role assignment
Utilities.Log("Creating a Contributor role assignment " + raName + " for the service principal...");
SdkContext.DelayProvider.Delay(15000);
IRoleAssignment roleAssignment = authenticated.RoleAssignments
.Define(raName)
.ForServicePrincipal(servicePrincipal)
.WithBuiltInRole(BuiltInRole.Contributor)
.WithSubscriptionScope(authenticated.Subscriptions.List().First<ISubscription>().SubscriptionId)
.Create();
Utilities.Log("Created role assignment " + raName + ".");
Utilities.Print(roleAssignment);
// ============================================================
// Verify the credentials are valid
Utilities.Log("Verifying password credential " + passwordName1 + " is valid...");
AzureCredentials testCredential = new AzureCredentialsFactory().FromServicePrincipal(
servicePrincipal.ApplicationId, password1, authenticated.TenantId, environment);
try
{
Azure.Authenticate(testCredential).WithDefaultSubscription();
Utilities.Log("Verified " + passwordName1 + " is valid.");
}
catch (Exception e)
{
Utilities.Log("Failed to verify " + passwordName1 + " is valid. Exception: " + e.Message);
}
Utilities.Log("Verifying password credential " + passwordName2 + " is valid...");
testCredential = new AzureCredentialsFactory().FromServicePrincipal(
servicePrincipal.ApplicationId, password2, authenticated.TenantId, environment);
try
{
Azure.Authenticate(testCredential).WithDefaultSubscription();
Utilities.Log("Verified " + passwordName2 + " is valid.");
}
catch (Exception e)
{
Utilities.Log("Failed to verify " + passwordName2 + " is valid. Exception: " + e.Message);
}
Utilities.Log("Verifying certificate credential " + certName1 + " is valid...");
testCredential = new AzureCredentialsFactory().FromServicePrincipal(
servicePrincipal.ApplicationId,
Path.Combine(Utilities.ProjectPath, "Asset", "NetworkTestCertificate1.pfx"),
"Abc123",
authenticated.TenantId,
environment);
try
{
Azure.Authenticate(testCredential).WithDefaultSubscription();
Utilities.Log("Verified " + certName1 + " is valid.");
}
catch (Exception e)
{
Utilities.Log("Failed to verify " + certName1 + " is valid. Exception: " + e.Message);
}
// ============================================================
// Revoke access of the 1st password credential
Utilities.Log("Revoking access for password credential " + passwordName1 + "...");
servicePrincipal.Update()
.WithoutCredential(passwordName1)
.Apply();
SdkContext.DelayProvider.Delay(15000);
Utilities.Log("Credential revoked.");
// ============================================================
// Verify the revoked password credential is no longer valid
Utilities.Log("Verifying password credential " + passwordName1 + " is revoked...");
testCredential = new AzureCredentialsFactory().FromServicePrincipal(
servicePrincipal.ApplicationId, password1, authenticated.TenantId, environment);
try
{
Azure.Authenticate(testCredential).WithDefaultSubscription();
Utilities.Log("Failed to verify " + passwordName1 + " is revoked.");
}
catch (Exception e)
{
Utilities.Log("Verified " + passwordName1 + " is revoked. Exception: " + e.Message);
}
// ============================================================
// Revoke the role assignment
Utilities.Log("Revoking role assignment " + raName + "...");
authenticated.RoleAssignments.DeleteById(roleAssignment.Id);
SdkContext.DelayProvider.Delay(5000);
// ============================================================
// Verify the revoked password credential is no longer valid
Utilities.Log("Verifying password credential " + passwordName2 + " has no access to subscription...");
testCredential = new AzureCredentialsFactory().FromServicePrincipal(
servicePrincipal.ApplicationId, password2, authenticated.TenantId, environment);
try
{
Azure.Authenticate(testCredential).WithDefaultSubscription()
.ResourceGroups.List();
Utilities.Log("Failed to verify " + passwordName2 + " has no access to subscription.");
}
catch (Exception e)
{
Utilities.Log("Verified " + passwordName2 + " has no access to subscription. Exception: " + e.Message);
}
}
catch (Exception f)
{
Utilities.Log(f.Message);
}
finally
{
try
{
Utilities.Log("Deleting application: " + appName);
authenticated.ServicePrincipals.DeleteById(servicePrincipalId);
Utilities.Log("Deleted application: " + appName);
}
catch (Exception e)
{
Utilities.Log("Did not create applications in Azure. No clean up is necessary. Exception: " + e.Message);
}
}
}
public static void Main(string[] args)
{
try
{
//=================================================================
// Authenticate
AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var authenticated = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials);
RunSample(authenticated, credentials.Environment);
Console.ReadLine();
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
}