-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generate Azure Blob Signature.groovy
145 lines (132 loc) · 5.31 KB
/
Generate Azure Blob Signature.groovy
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
/*
Documents:
https://docs.microsoft.com/en-us/rest/api/storageservices/list-blobs
https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob
https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob
https://docs.microsoft.com/en-us/rest/api/storageservices/delete-blob
https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key
*/
// Import required classes
import java.util.Properties;
import java.io.InputStream;
import com.boomi.execution.ExecutionUtil;
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.nio.charset.StandardCharsets
// Define variables
String accountName = ExecutionUtil.getDynamicProcessProperty("Account Name DPP");
String key = ExecutionUtil.getDynamicProcessProperty("Access Key DPP");
String container = ExecutionUtil.getDynamicProcessProperty("Container Name DPP");
String httpVerb = ExecutionUtil.getDynamicProcessProperty("HTTP Verb DPP");
String operation = ExecutionUtil.getDynamicProcessProperty("Operation DPP");
String contentType = ExecutionUtil.getDynamicProcessProperty("Content Type DPP") ?: "";
// Get input data stream
for (int i = 0; i < dataContext.getDataCount(); i++) {
InputStream is = dataContext.getStream(i);
Properties props = dataContext.getProperties(i);
// Set the value of variables
String contentLength = is.text.length();
String blobName = props.getProperty("document.dynamic.userdefined.Blob Name DDP") ?: "";
String longDate = setLongDate();
String stringToSign = getStringToSign(operation, httpVerb, contentLength, contentType, longDate, accountName, container, blobName)
String authorization = "SharedKey " + accountName + ":" + getHMAC256(key, stringToSign);
// Reset the value of variables
is.reset();
// Set headers for HTTP request
props.setProperty("document.dynamic.userdefined.Authorization", authorization);
props.setProperty("document.dynamic.userdefined.x-ms-date", longDate);
dataContext.storeStream(is, props);
}
// Generate Signature
String getHMAC256(String base64Key, String stringToSign) {
byte[] key = Base64.getDecoder().decode(base64Key);
Mac hmacSHA256 = Mac.getInstance("HmacSHA256");
hmacSHA256.init(new SecretKeySpec(key, "HmacSHA256"));
byte[] utf8Bytes = stringToSign.getBytes(StandardCharsets.UTF_8);
byte[] output = hmacSHA256.doFinal(utf8Bytes);
return Base64.getEncoder().encodeToString(output);
}
// Generate Current Date
String setLongDate() {
DateFormat longDateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss zz");
longDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date();
return longDateFormat.format(date);
}
// Generate StringToSign value
String getStringToSign(String operation, String httpVerb, String contentLength, String contentType, String longDate, String accountName, String container, String blobName) {
String stringToSign
switch (operation) {
case "GET": stringToSign = httpVerb + "\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"x-ms-date:" + longDate + "\n" +
"x-ms-version:2020-10-02\n" +
"/" + accountName + "/" + container + "/" + blobName;
break;
case "CREATE": stringToSign = httpVerb + "\n" +
"\n" +
"\n" + contentLength +
"\n" +
"\n" + contentType +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"x-ms-blob-type:BlockBlob" + "\n" +
"x-ms-date:" + longDate + "\n" +
"x-ms-version:2020-10-02\n" +
"/" + accountName + "/" + container + "/" + blobName;
break;
case "DELETE": stringToSign = httpVerb + "\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"x-ms-date:" + longDate + "\n" +
"x-ms-version:2020-10-02\n" +
"/" + accountName + "/" + container + "/" + blobName
break;
case "LIST": stringToSign = httpVerb + "\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"x-ms-date:" + longDate + "\n" +
"x-ms-version:2020-10-02\n" +
"/" + accountName + "/" + container + "\n" +
"comp:list\n" +
"restype:container";
break;
default: throw new Exception(httpVerb + " is not a valid Operation DPP. Valid values are GET, CREATE, DELETE, or LIST.");
}
return stringToSign;
}