This repository was archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsesclient.native.cpp
More file actions
217 lines (183 loc) · 6.28 KB
/
sesclient.native.cpp
File metadata and controls
217 lines (183 loc) · 6.28 KB
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
#include "stdafx.h"
namespace
{
void ShowUsage(const char* exeName)
{
std::cerr
<< exeName << ":" << std::endl
<< "Contacts the specified Azure Batch software entitlement server to verify the provided token." << std::endl
<< std::endl
<< "Mandatory parameters:" << std::endl
<< " --url <software entitlement server URL>" << std::endl
<< " --token <software entitlement token to pass to the server>" << std::endl
<< " --application <name of the license ID being requested>" << std::endl
<< std::endl
<< "Optional parameters:" << std::endl
<< " --thumbprint <thumbprint of a certificate expected in the server's SSL certificate chain>" << std::endl
<< " --common-name <common name of the certificate with the specified thumbprint>" << std::endl;
}
static const std::array<std::string, 3> mandatoryParameterNames = {
"--url",
"--token",
"--application"
};
static const std::array<std::string, 2> optionalParameterNames = {
"--thumbprint",
"--common-name"
};
struct Initializer
{
Initializer()
{
int err = Microsoft::Azure::Batch::SoftwareEntitlement::Init();
if (err != 0)
{
throw std::runtime_error(
"Microsoft::Azure::Batch::SoftwareEntitlement::Init failed with error " +
std::to_string(err)
);
}
}
~Initializer()
{
Microsoft::Azure::Batch::SoftwareEntitlement::Cleanup();
}
};
struct ParameterParser
{
public:
bool parse(int argc, char** argv)
{
if ((argc % 2) == 1)
{
// We have pairs of parameters, collect them into our map
while (argc != 1)
{
auto value = argv[--argc];
auto key = argv[--argc];
_parameters.emplace(key, value);
}
}
if (_parameters.size() == 0)
{
// Need to show usage to the end user
return true;
}
checkForMandatoryParameters(_parameters);
checkForExtraParameters(_parameters);
return false;
}
bool contains(const std::string& name) const
{
return _parameters.find(name) != _parameters.end();
}
std::string find(const std::string& name) const
{
return _parameters.find(name)->second;
}
bool hasConfigurationError() const
{
return _hasConfigurationError;
}
private:
bool _hasConfigurationError = false;
std::unordered_map<std::string, std::string> _parameters;
void checkForMandatoryParameters(const std::unordered_map<std::string, std::string>& parameters)
{
for (const auto& param : mandatoryParameterNames)
{
if (parameters.find(param) == parameters.end())
{
std::cerr << "Missing mandatory parameter " << param << std::endl;
_hasConfigurationError = true;
}
}
}
void checkForExtraParameters(const std::unordered_map<std::string, std::string>& parameters)
{
for (const auto& parameter : parameters)
{
if (std::find(mandatoryParameterNames.begin(), mandatoryParameterNames.end(), parameter.first) != mandatoryParameterNames.end()) {
continue;
}
if (std::find(optionalParameterNames.begin(), optionalParameterNames.end(), parameter.first) != optionalParameterNames.end()) {
continue;
}
std::cerr << "Unexpected additional parameter: " << parameter.first << " " << parameter.second << std::endl;
_hasConfigurationError = true;
}
}
};
bool configureConnection(const ParameterParser& parameters)
{
if (!parameters.contains("--thumbprint")
&& !parameters.contains("--common-name"))
{
// We have neither value - and that's ok
return true;
}
if (!parameters.contains("--thumbprint"))
{
std::cerr << "--thumbprint must also be used when --common-name is used" << std::endl;
return false;
}
if (!parameters.contains("--common-name"))
{
std::cerr << "--common-name must also be used when --thumbprint is used" << std::endl;
return false;
}
auto thumbprintParameter = parameters.find("--thumbprint");
auto commonNameParameter = parameters.find("--common-name");
Microsoft::Azure::Batch::SoftwareEntitlement::AddSslCertificate(thumbprintParameter, commonNameParameter);
return true;
}
std::string readToken(ParameterParser& parameters)
{
auto token = parameters.find("--token");
if (token == "-")
{
// Read the token from stdin.
if (!std::getline(std::cin, token))
{
throw std::runtime_error("Failed to read token from stdin");
}
}
return token;
}
}
int main(int argc, char** argv)
{
try
{
Initializer init;
ParameterParser parser;
auto shouldShowUsage = parser.parse(argc, argv);
if (shouldShowUsage)
{
ShowUsage(argv[0]);
return -1;
}
if (parser.hasConfigurationError())
{
return -EINVAL;
}
auto token = readToken(parser);
auto connectionConfigured = configureConnection(parser);
if (!connectionConfigured)
{
return -EINVAL;
}
auto entitlement = Microsoft::Azure::Batch::SoftwareEntitlement::GetEntitlement(
parser.find("--url"),
token,
parser.find("--application")
);
std::cout << entitlement->Id() << std::endl;
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return -1;
}
return 0;
}