Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gssapi.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. 'use strict';
  2. const f = require('util').format;
  3. const Query = require('../connection/commands').Query;
  4. const MongoError = require('../error').MongoError;
  5. const retrieveKerberos = require('../utils').retrieveKerberos;
  6. var AuthSession = function(db, username, password, options) {
  7. this.db = db;
  8. this.username = username;
  9. this.password = password;
  10. this.options = options;
  11. };
  12. AuthSession.prototype.equal = function(session) {
  13. return (
  14. session.db === this.db &&
  15. session.username === this.username &&
  16. session.password === this.password
  17. );
  18. };
  19. /**
  20. * Creates a new GSSAPI authentication mechanism
  21. * @class
  22. * @return {GSSAPI} A cursor instance
  23. */
  24. var GSSAPI = function(bson) {
  25. this.bson = bson;
  26. this.authStore = [];
  27. };
  28. /**
  29. * Authenticate
  30. * @method
  31. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  32. * @param {[]Connections} connections Connections to authenticate using this authenticator
  33. * @param {string} db Name of the database
  34. * @param {string} username Username
  35. * @param {string} password Password
  36. * @param {authResultCallback} callback The callback to return the result from the authentication
  37. * @return {object}
  38. */
  39. GSSAPI.prototype.auth = function(server, connections, db, username, password, options, callback) {
  40. var self = this;
  41. let kerberos;
  42. try {
  43. kerberos = retrieveKerberos();
  44. } catch (e) {
  45. return callback(e, null);
  46. }
  47. // TODO: remove this once we fix URI parsing
  48. var gssapiServiceName = options['gssapiservicename'] || options['gssapiServiceName'] || 'mongodb';
  49. // Total connections
  50. var count = connections.length;
  51. if (count === 0) return callback(null, null);
  52. // Valid connections
  53. var numberOfValidConnections = 0;
  54. var errorObject = null;
  55. // For each connection we need to authenticate
  56. while (connections.length > 0) {
  57. // Execute MongoCR
  58. var execute = function(connection) {
  59. // Start Auth process for a connection
  60. GSSAPIInitialize(
  61. self,
  62. kerberos.processes.MongoAuthProcess,
  63. db,
  64. username,
  65. password,
  66. db,
  67. gssapiServiceName,
  68. server,
  69. connection,
  70. options,
  71. function(err, r) {
  72. // Adjust count
  73. count = count - 1;
  74. // If we have an error
  75. if (err) {
  76. errorObject = err;
  77. } else if (r.result['$err']) {
  78. errorObject = r.result;
  79. } else if (r.result['errmsg']) {
  80. errorObject = r.result;
  81. } else {
  82. numberOfValidConnections = numberOfValidConnections + 1;
  83. }
  84. // We have authenticated all connections
  85. if (count === 0 && numberOfValidConnections > 0) {
  86. // Store the auth details
  87. addAuthSession(self.authStore, new AuthSession(db, username, password, options));
  88. // Return correct authentication
  89. callback(null, true);
  90. } else if (count === 0) {
  91. if (errorObject == null)
  92. errorObject = new MongoError(f('failed to authenticate using mongocr'));
  93. callback(errorObject, false);
  94. }
  95. }
  96. );
  97. };
  98. var _execute = function(_connection) {
  99. process.nextTick(function() {
  100. execute(_connection);
  101. });
  102. };
  103. _execute(connections.shift());
  104. }
  105. };
  106. //
  107. // Initialize step
  108. var GSSAPIInitialize = function(
  109. self,
  110. MongoAuthProcess,
  111. db,
  112. username,
  113. password,
  114. authdb,
  115. gssapiServiceName,
  116. server,
  117. connection,
  118. options,
  119. callback
  120. ) {
  121. // Create authenticator
  122. var mongo_auth_process = new MongoAuthProcess(
  123. connection.host,
  124. connection.port,
  125. gssapiServiceName,
  126. options
  127. );
  128. // Perform initialization
  129. mongo_auth_process.init(username, password, function(err) {
  130. if (err) return callback(err, false);
  131. // Perform the first step
  132. mongo_auth_process.transition('', function(err, payload) {
  133. if (err) return callback(err, false);
  134. // Call the next db step
  135. MongoDBGSSAPIFirstStep(
  136. self,
  137. mongo_auth_process,
  138. payload,
  139. db,
  140. username,
  141. password,
  142. authdb,
  143. server,
  144. connection,
  145. callback
  146. );
  147. });
  148. });
  149. };
  150. //
  151. // Perform first step against mongodb
  152. var MongoDBGSSAPIFirstStep = function(
  153. self,
  154. mongo_auth_process,
  155. payload,
  156. db,
  157. username,
  158. password,
  159. authdb,
  160. server,
  161. connection,
  162. callback
  163. ) {
  164. // Build the sasl start command
  165. var command = {
  166. saslStart: 1,
  167. mechanism: 'GSSAPI',
  168. payload: payload,
  169. autoAuthorize: 1
  170. };
  171. // Write the commmand on the connection
  172. server(
  173. connection,
  174. new Query(self.bson, '$external.$cmd', command, {
  175. numberToSkip: 0,
  176. numberToReturn: 1
  177. }),
  178. function(err, r) {
  179. if (err) return callback(err, false);
  180. var doc = r.result;
  181. // Execute mongodb transition
  182. mongo_auth_process.transition(r.result.payload, function(err, payload) {
  183. if (err) return callback(err, false);
  184. // MongoDB API Second Step
  185. MongoDBGSSAPISecondStep(
  186. self,
  187. mongo_auth_process,
  188. payload,
  189. doc,
  190. db,
  191. username,
  192. password,
  193. authdb,
  194. server,
  195. connection,
  196. callback
  197. );
  198. });
  199. }
  200. );
  201. };
  202. //
  203. // Perform first step against mongodb
  204. var MongoDBGSSAPISecondStep = function(
  205. self,
  206. mongo_auth_process,
  207. payload,
  208. doc,
  209. db,
  210. username,
  211. password,
  212. authdb,
  213. server,
  214. connection,
  215. callback
  216. ) {
  217. // Build Authentication command to send to MongoDB
  218. var command = {
  219. saslContinue: 1,
  220. conversationId: doc.conversationId,
  221. payload: payload
  222. };
  223. // Execute the command
  224. // Write the commmand on the connection
  225. server(
  226. connection,
  227. new Query(self.bson, '$external.$cmd', command, {
  228. numberToSkip: 0,
  229. numberToReturn: 1
  230. }),
  231. function(err, r) {
  232. if (err) return callback(err, false);
  233. var doc = r.result;
  234. // Call next transition for kerberos
  235. mongo_auth_process.transition(doc.payload, function(err, payload) {
  236. if (err) return callback(err, false);
  237. // Call the last and third step
  238. MongoDBGSSAPIThirdStep(
  239. self,
  240. mongo_auth_process,
  241. payload,
  242. doc,
  243. db,
  244. username,
  245. password,
  246. authdb,
  247. server,
  248. connection,
  249. callback
  250. );
  251. });
  252. }
  253. );
  254. };
  255. var MongoDBGSSAPIThirdStep = function(
  256. self,
  257. mongo_auth_process,
  258. payload,
  259. doc,
  260. db,
  261. username,
  262. password,
  263. authdb,
  264. server,
  265. connection,
  266. callback
  267. ) {
  268. // Build final command
  269. var command = {
  270. saslContinue: 1,
  271. conversationId: doc.conversationId,
  272. payload: payload
  273. };
  274. // Execute the command
  275. server(
  276. connection,
  277. new Query(self.bson, '$external.$cmd', command, {
  278. numberToSkip: 0,
  279. numberToReturn: 1
  280. }),
  281. function(err, r) {
  282. if (err) return callback(err, false);
  283. mongo_auth_process.transition(null, function(err) {
  284. if (err) return callback(err, null);
  285. callback(null, r);
  286. });
  287. }
  288. );
  289. };
  290. // Add to store only if it does not exist
  291. var addAuthSession = function(authStore, session) {
  292. var found = false;
  293. for (var i = 0; i < authStore.length; i++) {
  294. if (authStore[i].equal(session)) {
  295. found = true;
  296. break;
  297. }
  298. }
  299. if (!found) authStore.push(session);
  300. };
  301. /**
  302. * Remove authStore credentials
  303. * @method
  304. * @param {string} db Name of database we are removing authStore details about
  305. * @return {object}
  306. */
  307. GSSAPI.prototype.logout = function(dbName) {
  308. this.authStore = this.authStore.filter(function(x) {
  309. return x.db !== dbName;
  310. });
  311. };
  312. /**
  313. * Re authenticate pool
  314. * @method
  315. * @param {{Server}|{ReplSet}|{Mongos}} server Topology the authentication method is being called on
  316. * @param {[]Connections} connections Connections to authenticate using this authenticator
  317. * @param {authResultCallback} callback The callback to return the result from the authentication
  318. * @return {object}
  319. */
  320. GSSAPI.prototype.reauthenticate = function(server, connections, callback) {
  321. var authStore = this.authStore.slice(0);
  322. var count = authStore.length;
  323. if (count === 0) return callback(null, null);
  324. // Iterate over all the auth details stored
  325. for (var i = 0; i < authStore.length; i++) {
  326. this.auth(
  327. server,
  328. connections,
  329. authStore[i].db,
  330. authStore[i].username,
  331. authStore[i].password,
  332. authStore[i].options,
  333. function(err) {
  334. count = count - 1;
  335. // Done re-authenticating
  336. if (count === 0) {
  337. callback(err, null);
  338. }
  339. }
  340. );
  341. }
  342. };
  343. /**
  344. * This is a result from a authentication strategy
  345. *
  346. * @callback authResultCallback
  347. * @param {error} error An error object. Set to null if no error present
  348. * @param {boolean} result The result of the authentication process
  349. */
  350. module.exports = GSSAPI;