-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSimpleSasl.cpp
More file actions
256 lines (220 loc) · 8.54 KB
/
SimpleSasl.cpp
File metadata and controls
256 lines (220 loc) · 8.54 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "infinispan/hotrod/ConfigurationBuilder.h"
#include "infinispan/hotrod/RemoteCacheManager.h"
#include "infinispan/hotrod/RemoteCache.h"
#include "infinispan/hotrod/Version.h"
#include "infinispan/hotrod/JBasicMarshaller.h"
#if !defined _WIN32 && !defined _WIN64
#include <sasl/saslplug.h>
#include <krb5.h>
#include <err.h>
#endif
#include <stdlib.h>
#include <iostream>
#include <memory>
#include <typeinfo>
#include <thread>
#include <future>
#include <chrono>
#include <cstdlib>
// For CTest: return 0 if all tests pass, non-zero otherwise.
using namespace infinispan::hotrod;
namespace infinispan {
namespace hotrod {
namespace transport {
class MyRoundRobinBalancingStrategy: public FailOverRequestBalancingStrategy {
public:
MyRoundRobinBalancingStrategy() :
index(0) {
}
static FailOverRequestBalancingStrategy *newInstance() {
return new MyRoundRobinBalancingStrategy();
}
void setServers(const std::vector<transport::InetSocketAddress> &s) {
servers = s;
// keep the old index if possible so that we don't produce more requests for the first server
if (index >= servers.size()) {
index = 0;
}
}
~MyRoundRobinBalancingStrategy() {
}
const transport::InetSocketAddress &getServerByIndex(size_t pos) {
const transport::InetSocketAddress &server = servers[pos];
return server;
}
private:
std::vector<transport::InetSocketAddress> servers;
size_t index;
const transport::InetSocketAddress &nextServer(const std::set<transport::InetSocketAddress>& failedServers) {
for (unsigned int i = 0; i <= servers.size(); i++) {
const transport::InetSocketAddress &server = getServerByIndex(index++);
if (failedServers.empty() || failedServers.find(server) != failedServers.end()
|| i > failedServers.size()) {
if (index >= servers.size()) {
index = 0;
}
}
return server;
}
throw Exception("Bad news, no server available.");
}
};
}
}
}
template<class T>
void assert_not_null(const std::string& message, int line, const std::unique_ptr<T>& pointer) {
if (pointer.get() == 0) {
std::cerr << message << ":" << line << std::endl;
std::cerr.flush();
throw std::exception();
}
}
#if !defined _WIN32 && !defined _WIN64
int kinit();
void kdestroy();
#endif
static char *simple_data; // plain
#if !defined _WIN32 && !defined _WIN64
static char realm_data[] = "applicationRealm";
#else
static char realm_data[] = "ApplicationRealm";
#endif
static char path_data[] = "/usr/lib64/sasl2";
static int simple(void* /* context */, int id, const char **result, unsigned *len) {
*result = simple_data;
if (len)
*len = strlen(simple_data);
return SASL_OK;
}
static int getrealm(void* /* context */, int id, const char **result, unsigned *len) {
*result = realm_data;
if (len)
*len = strlen(realm_data);
return SASL_OK;
}
#define PLUGINDIR "/usr/lib64/sasl2"
static int getpath(void *context, const char ** path) {
const char *searchpath = (const char *) context;
if (!path)
return SASL_BADPARAM;
*path = PLUGINDIR;
return SASL_OK;
}
static char *secret_data;
static int getsecret(void* /* conn */, void* /* context */, int id, sasl_secret_t **psecret) {
size_t len;
static sasl_secret_t *x;
len = strlen(secret_data);
x = (sasl_secret_t *) realloc(x, sizeof(sasl_secret_t) + len);
x->len = len;
strcpy((char *) x->data, secret_data);
*psecret = x;
return SASL_OK;
}
static std::vector<sasl_callback_t> callbackHandler { { SASL_CB_USER, (sasl_callback_ft) &simple, NULL }, {
SASL_CB_AUTHNAME, (sasl_callback_ft) &simple, NULL }, { SASL_CB_PASS, (sasl_callback_ft) &getsecret, NULL }, {
SASL_CB_GETREALM, (sasl_callback_ft) &getrealm, NULL }, { SASL_CB_GETPATH, (sasl_callback_ft) &getpath, NULL }, {
SASL_CB_LIST_END, NULL, NULL } };
#if !defined _WIN32 && !defined _WIN64
int kinit();
void kdestroy();
#endif
int main(int argc, char** argv) {
int result = 0;
{
ConfigurationBuilder builder;
simple_data = "writer";
secret_data = "somePassword";
builder.addServer().host(argc > 1 ? argv[1] : "127.0.0.1").port(argc > 2 ? atoi(argv[2]) : 11222);
builder.protocolVersion(Configuration::PROTOCOL_VERSION_24);
builder.security().authentication().saslMechanism("PLAIN").serverFQDN(
"node0").callbackHandler(callbackHandler).enable();
builder.balancingStrategyProducer(nullptr);
RemoteCacheManager cacheManager(builder.build(), false);
BasicMarshaller<std::string> *km = new BasicMarshaller<std::string>();
BasicMarshaller<std::string> *vm = new BasicMarshaller<std::string>();
RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km,
&Marshaller<std::string>::destroy, vm, &Marshaller<std::string>::destroy, std::string("authCache"));
cacheManager.start();
cache.put("key", "value");
try {
std::shared_ptr<std::string> ret(cache.get("key"));
std::cerr << "FAIL: 'writer' should not read" << std::endl;
return -1;
} catch (Exception& ex) {
}
std::cout << "PASS: 'PLAIN' sasl authorization" << std::endl;
cacheManager.stop();
}
{
ConfigurationBuilder builder;
simple_data = "reader";
secret_data = "password";
builder.addServer().host(argc > 1 ? argv[1] : "127.0.0.1").port(argc > 2 ? atoi(argv[2]) : 11222);
builder.protocolVersion(Configuration::PROTOCOL_VERSION_24);
builder.security().authentication().saslMechanism("DIGEST-MD5").serverFQDN(
"node0").callbackHandler(callbackHandler).enable();
builder.balancingStrategyProducer(nullptr);
RemoteCacheManager cacheManager(builder.build(), false);
BasicMarshaller<std::string> *km = new BasicMarshaller<std::string>();
BasicMarshaller<std::string> *vm = new BasicMarshaller<std::string>();
RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km,
&Marshaller<std::string>::destroy, vm, &Marshaller<std::string>::destroy, std::string("authCache"));
cacheManager.start();
std::shared_ptr<std::string> ret(cache.get("key"));
try {
cache.put("key", "value");
std::cerr << "FAIL: 'reader' should not write" << std::endl;
return -1;
} catch (Exception& ex) {
}
std::cout << "PASS: 'DIGEST-MD5' sasl authorization" << std::endl;
cacheManager.stop();
}
#if !defined _WIN32 && !defined _WIN64
{
kinit();
ConfigurationBuilder builder;
simple_data = "[email protected]";
secret_data = "lessStrongPassword";
builder.addServer().host(argc > 1 ? argv[1] : "127.0.0.1").port(argc > 2 ? atoi(argv[2]) : 11222);
builder.protocolVersion(Configuration::PROTOCOL_VERSION_24);
builder.security().authentication().saslMechanism("GSSAPI").serverFQDN(
"node0").callbackHandler(callbackHandler).enable();
builder.balancingStrategyProducer(nullptr);
RemoteCacheManager cacheManager(builder.build(), false);
BasicMarshaller<std::string> *km = new BasicMarshaller<std::string>();
BasicMarshaller<std::string> *vm = new BasicMarshaller<std::string>();
RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km,
&Marshaller<std::string>::destroy, vm, &Marshaller<std::string>::destroy, std::string("authCache"));
cacheManager.start();
try {
cache.put("key", "value");
std::shared_ptr<std::string> ret(cache.get("key"));
result = 0;
} catch (Exception& ex) {
std::cerr << "FAIL: 'supervisor' should read and write" << std::endl;
result = -1;
}
cacheManager.stop();
std::cout << "PASS: 'GSSAPI' sasl authorization" << std::endl;
kdestroy();
}
#endif
return result;
}
#if !defined _WIN32 && !defined _WIN64
krb5_context context;
krb5_creds creds;
krb5_principal client_princ = NULL;
int kinit() {
// Delegate Kerberos setup to the system
setenv("KRB5CCNAME", "krb5cc_hotrod", 1);
setenv("KRB5_CONFIG", "krb5.conf", 1);
std::system("echo lessStrongPassword | kinit -c krb5cc_hotrod [email protected]");
}
void kdestroy() {
std::system("kdestroy");
}
#endif