Skip to content

Commit 986d643

Browse files
authored
[DataBoxEdge] data box edge sdk migrate (#27155)
* generate DataBoxEdge, add Microsoft.Azure.PowerShell.DataBoxEdge.Management.Sdk * generate DataBoxEdge by autorest powershell * Update ChangeLog.md * fix payload-flattening-threshold * Update ChangeLog.md * test record and case change type * Update TestCreateEdgeStorageContainer.json * add license
1 parent d1d108c commit 986d643

File tree

223 files changed

+36438
-4878
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+36438
-4878
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Management.DataBoxEdge
16+
{
17+
using System;
18+
using System.IO;
19+
using System.Security.Cryptography;
20+
using System.Security.Cryptography.X509Certificates;
21+
using System.Text;
22+
23+
/// <summary>
24+
/// The crypto helper.
25+
/// </summary>
26+
public class CryptoUtilities
27+
{
28+
/// <summary>
29+
/// The salt for generating encryption keys.
30+
/// </summary>
31+
private static readonly byte[] Salt = Encoding.ASCII.GetBytes("o6806642kbM7c5");
32+
33+
/// <summary>
34+
/// The AES algorithm is used to decrypt the given cipherText.
35+
/// </summary>
36+
/// <param name="cipherText">The cipher text.</param>
37+
/// <param name="sharedSecret">The shared secret.</param>
38+
/// <returns>The decrypted secret in pain text.</returns>
39+
public static string DecryptCipherAES(string cipherText, string sharedSecret)
40+
{
41+
if (string.IsNullOrEmpty(cipherText))
42+
{
43+
return cipherText;
44+
}
45+
46+
Aes aesAlg = null;
47+
48+
string plaintext = null;
49+
50+
// generate the key from the shared secret and the salt
51+
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, Salt);
52+
53+
// Create the streams used for decryption.
54+
byte[] bytes = Convert.FromBase64String(cipherText);
55+
using (MemoryStream memoryDecrypt = new MemoryStream(bytes))
56+
{
57+
aesAlg = Aes.Create();
58+
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
59+
60+
// Get the initialization vector from the encrypted stream
61+
aesAlg.IV = ReadByteArray(memoryDecrypt);
62+
63+
// Create a decrytor to perform the stream transform.
64+
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
65+
using (CryptoStream cryptoDecrypt = new CryptoStream(memoryDecrypt, decryptor, CryptoStreamMode.Read))
66+
{
67+
using (StreamReader streamDecrypt = new StreamReader(cryptoDecrypt))
68+
{
69+
// Read the decrypted bytes from the decrypting stream and place them in a string.
70+
plaintext = streamDecrypt.ReadToEnd();
71+
}
72+
}
73+
}
74+
75+
return plaintext;
76+
}
77+
78+
public static string DecryptStringAES(string cipherText, string sharedSecret)
79+
{
80+
if (string.IsNullOrEmpty(cipherText))
81+
return cipherText;
82+
83+
Aes aesAlg = null;
84+
string plaintext = null;
85+
86+
// generate the key from the shared secret and the salt
87+
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, Salt);
88+
89+
// Create the streams used for decryption.
90+
byte[] bytes = Convert.FromBase64String(cipherText);
91+
using (MemoryStream msDecrypt = new MemoryStream(bytes))
92+
{
93+
aesAlg = Aes.Create();
94+
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
95+
// Get the initialization vector from the encrypted stream
96+
aesAlg.IV = ReadByteArray(msDecrypt);
97+
// Create a decrytor to perform the stream transform.
98+
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
99+
using (CryptoStream csDecrypt =
100+
new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
101+
{
102+
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
103+
104+
// Read the decrypted bytes from the decrypting stream
105+
// and place them in a string.
106+
plaintext = srDecrypt.ReadToEnd();
107+
}
108+
}
109+
110+
return plaintext;
111+
}
112+
113+
/// <summary>
114+
/// This method encrypts a given secret using the public certificate.
115+
/// </summary>
116+
/// <param name="plainText">The secret in plain text.</param>
117+
/// <param name="publicCertificate">The public certificate to be used for encryption.</param>
118+
/// <returns>The encrypted secret.</returns>
119+
public static string EncryptSecretRSAPKCS(string plainText, string publicCertificate)
120+
{
121+
string encryptedSecret = null;
122+
encryptedSecret = EncryptStringRsaPkcs1v15(plainText, publicCertificate);
123+
return encryptedSecret;
124+
}
125+
126+
public static string EncryptStringRsaPkcs1v15(string plaintext, string encodedCertificate)
127+
{
128+
X509Certificate2 cert = new X509Certificate2(Convert.FromBase64String(encodedCertificate));
129+
if (string.IsNullOrEmpty(plaintext) || cert == null)
130+
{
131+
return null;
132+
}
133+
134+
byte[] textBytes = Encoding.UTF8.GetBytes(plaintext);
135+
byte[] encryptedTextBytes;
136+
137+
// Create a new instance of RSACryptoServiceProvider, and encrypt the passed byte array and specify OAEP padding false to use PKCS#1 V1.5 padding.
138+
#if FullNetFx
139+
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key;
140+
encryptedTextBytes = rsa.Encrypt(textBytes, false);
141+
#else
142+
RSA rsa = cert.GetRSAPublicKey();
143+
encryptedTextBytes = rsa.Encrypt(textBytes, RSAEncryptionPadding.Pkcs1);
144+
#endif
145+
var encryptedBase64 = Convert.ToBase64String(encryptedTextBytes);
146+
return encryptedBase64;
147+
}
148+
149+
/// <summary>
150+
/// Helper method to read byte array from a stream.
151+
/// </summary>
152+
/// <param name="s">The stream.</param>
153+
/// <returns>The byte array.</returns>
154+
private static byte[] ReadByteArray(Stream s)
155+
{
156+
byte[] rawLength = new byte[sizeof(int)];
157+
if (s.Read(rawLength, 0, rawLength.Length) != rawLength.Length)
158+
{
159+
throw new Exception("Stream did not contain properly formatted byte array");
160+
}
161+
162+
byte[] buffer = new byte[BitConverter.ToInt32(rawLength, 0)];
163+
if (s.Read(buffer, 0, buffer.Length) != buffer.Length)
164+
{
165+
throw new Exception("Did not read byte array properly");
166+
}
167+
168+
return buffer;
169+
}
170+
}
171+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Management.DataBoxEdge.Models;
16+
using Microsoft.Rest;
17+
using Newtonsoft.Json.Linq;
18+
using Newtonsoft.Json;
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Linq;
22+
using System.Text;
23+
using System.Threading.Tasks;
24+
25+
namespace Microsoft.Azure.Management.DataBoxEdge
26+
{
27+
public static partial class ExtendedClientMethods
28+
{
29+
30+
/// <summary>
31+
/// Use this method to encrypt the user secrets (Storage Account Access Key, Volume Container Encryption Key etc.) using activation key
32+
/// </summary>
33+
/// <param name="deviceName">
34+
/// The resource name.
35+
/// </param>
36+
/// <param name="resourceGroupName">
37+
/// The resource group name.
38+
/// </param>
39+
/// <param name="plainTextSecret">
40+
/// The plain text secret.
41+
/// </param>
42+
/// <returns>
43+
/// The <see cref="AsymmetricEncryptedSecret"/>.
44+
/// </returns>
45+
/// <exception cref="ValidationException">
46+
/// </exception>
47+
/// <exception cref="InvalidOperationException">
48+
/// </exception>
49+
public static AsymmetricEncryptedSecret GetAsymmetricEncryptedSecretUsingActivationKey(
50+
this IDevicesOperations operations,
51+
string deviceName,
52+
string resourceGroupName,
53+
54+
string plainTextSecret,
55+
string activationKey)
56+
{
57+
if (string.IsNullOrWhiteSpace(activationKey))
58+
{
59+
throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.CannotBeNull, "activationKey");
60+
}
61+
62+
63+
64+
string channelIntegrationKey = GetChannelIntegrityKey(activationKey);
65+
return operations.GetAsymmetricEncryptedSecret(deviceName, resourceGroupName, plainTextSecret, channelIntegrationKey);
66+
}
67+
68+
/// <summary>
69+
/// Use this method to encrypt the user secrets (Storage Account Access Key, Volume Container Encryption Key etc.) using CIK
70+
/// </summary>
71+
/// <param name="deviceName">
72+
/// The resource name.
73+
/// </param>
74+
/// <param name="resourceGroupName">
75+
/// The resource group name.
76+
/// </param>
77+
/// <param name="plainTextSecret">
78+
/// The plain text secret.
79+
/// </param>
80+
/// <returns>
81+
/// The <see cref="AsymmetricEncryptedSecret"/>.
82+
/// </returns>
83+
/// <exception cref="ValidationException">
84+
/// </exception>
85+
/// <exception cref="InvalidOperationException">
86+
/// </exception>
87+
public static AsymmetricEncryptedSecret GetAsymmetricEncryptedSecret(
88+
this IDevicesOperations operations,
89+
string deviceName,
90+
string resourceGroupName,
91+
string plainTextSecret,
92+
string channelIntegrationKey)
93+
{
94+
if (string.IsNullOrWhiteSpace(plainTextSecret))
95+
{
96+
throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.CannotBeNull, "plainTextSecret");
97+
}
98+
99+
if (string.IsNullOrWhiteSpace(resourceGroupName))
100+
{
101+
throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.CannotBeNull, "resourceGroupName");
102+
}
103+
104+
if (string.IsNullOrWhiteSpace(deviceName))
105+
{
106+
throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.CannotBeNull, "resourceName");
107+
}
108+
109+
DataBoxEdgeDeviceExtendedInfo extendedInfo = operations.GetExtendedInformation(deviceName, resourceGroupName);
110+
string encryptionKey = extendedInfo.EncryptionKey;
111+
string encryptionKeyThumbprint = extendedInfo.EncryptionKeyThumbprint;
112+
113+
string ChannelEncryptionKey = CryptoUtilities.DecryptStringAES(encryptionKey, channelIntegrationKey);
114+
115+
var secret = new AsymmetricEncryptedSecret()
116+
{
117+
EncryptionAlgorithm = EncryptionAlgorithm.AES256,
118+
EncryptionCertThumbprint = encryptionKeyThumbprint,
119+
Value = CryptoUtilities.EncryptStringRsaPkcs1v15(plainTextSecret, ChannelEncryptionKey)
120+
};
121+
122+
return secret;
123+
}
124+
125+
126+
private static string GetChannelIntegrityKey(string activationKey)
127+
{
128+
string[] keys = activationKey.Split('#');
129+
string encodedString = keys[0];
130+
byte[] data = Convert.FromBase64String(encodedString);
131+
string decodedString = Encoding.UTF8.GetString(data);
132+
var jsondata = (JObject)JsonConvert.DeserializeObject(decodedString);
133+
string serviceDataIntegrityKey = jsondata["serviceDataIntegrityKey"].Value<string>();
134+
return serviceDataIntegrityKey;
135+
}
136+
}
137+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<PsModuleName>DataBoxEdge</PsModuleName>
4+
</PropertyGroup>
5+
<Import Project="$(MSBuildThisFileDirectory)..\..\Az.props" />
6+
<PropertyGroup>
7+
<TargetFramework>netstandard2.0</TargetFramework>
8+
<AssemblyName>Microsoft.Azure.PowerShell.DataBoxEdge.Management.Sdk</AssemblyName>
9+
<RootNamespace>Microsoft.Azure.Management.DataBoxEdge</RootNamespace>
10+
<NoWarn>$(NoWarn);CS0108;CS1573</NoWarn>
11+
</PropertyGroup>
12+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory).., build.proj))\src\Az.Post.props" />
13+
</Project>

0 commit comments

Comments
 (0)