summaryrefslogtreecommitdiffstats
path: root/app/access-control/controllers/ldap-controller.js
blob: 16992ecc461f9d3d4ccbc779db6602cb4272b98f (plain)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
 * Controller for LDAP
 *
 * @module app/access-control
 * @exports ldapController
 * @name ldapController
 */

window.angular && (function(angular) {
  'use strict';

  angular.module('app.accessControl').controller('ldapController', [
    '$scope', 'APIUtils', '$q', 'toastService',
    function($scope, APIUtils, $q, toastService) {
      $scope.loading = false;
      $scope.isSecure = false;
      $scope.ldapProperties = {};
      $scope.originalLdapProperties = {};
      $scope.submitted = false;
      $scope.roleGroups = [];
      $scope.roleGroupType = '';
      $scope.ldapCertExpiration = '';
      $scope.caCertExpiration = '';

      $scope.$on('$viewContentLoaded', function() {
        $scope.loadLdap();
      });

      $scope.loadLdap = function() {
        $scope.loading = true;
        $scope.submitted = false;
        const ldapAccountProperties =
            APIUtils.getAllUserAccountProperties().then(
                function(data) {
                  const serviceEnabled = data.LDAP.ServiceEnabled ||
                      data.ActiveDirectory.ServiceEnabled;
                  const ldapServiceEnabled = data.LDAP.ServiceEnabled;
                  const adServiceEnabled = data.ActiveDirectory.ServiceEnabled;
                  const enabledServiceType = getEnabledServiceType(data);
                  const serviceAddresses = getServiceAddresses(data);
                  const useSSL = getUseSsl(data);
                  const userName = getUsername(data);
                  const baseDistinguishedNames =
                      getBaseDistinguishedNames(data);
                  const groupsAttribute = getGroupsAttribute(data);
                  const usernameAttribute = getUsernameAttribute(data);
                  const authenticationType = getAuthenticationType(data);
                  const roleGroups = getRoleGroups(data);


                  return {
                    'ServiceEnabled': serviceEnabled,
                    'LDAPServiceEnabled': ldapServiceEnabled,
                    'ADServiceEnabled': adServiceEnabled,
                    'EnabledServiceType': enabledServiceType,
                    'ServiceAddresses': serviceAddresses,
                    'useSSL': useSSL,
                    'Username': userName,
                    'Password': null,
                    'BaseDistinguishedNames': baseDistinguishedNames,
                    'GroupsAttribute': groupsAttribute,
                    'UsernameAttribute': usernameAttribute,
                    'AuthenticationType': authenticationType,
                    'RoleGroups': roleGroups
                  };
                },
                function(error) {
                  console.log(JSON.stringify(error));
                });

        const ldapCertificate =
            getCertificate('/redfish/v1/AccountService/LDAP/Certificates');

        const caCertificate =
            getCertificate('/redfish/v1/Managers/bmc/Truststore/Certificates/');

        const promises =
            [ldapAccountProperties, ldapCertificate, caCertificate];
        $q.all(promises).then(function(results) {
          $scope.ldapProperties = results[0];
          $scope.originalLdapProperties = angular.copy(results[0]);
          $scope.roleGroupType = results[0].EnabledServiceType;
          $scope.roleGroups = results[0].RoleGroups;
          $scope.ldapCertificate = results[1];
          $scope.caCertificate = results[2];
          $scope.loading = false;
        });
      };

      /**
       * Save LDAP settings
       * Will be making two calls every time to accomodate the backend design
       * LDAP and ActiveDirectory changes can not be sent together when changing
       * from ActiveDirectory to LDAP
       */
      $scope.saveLdapSettings = function() {
        const enabledServiceType = $scope.ldapProperties.EnabledServiceType;
        const enabledServicePayload =
            createLdapEnableRequest(enabledServiceType, $scope.ldapProperties);
        const disabledServiceType =
            enabledServiceType == 'LDAP' ? 'ActiveDirectory' : 'LDAP';
        const disabledServicePayload =
            createLdapDisableRequest(disabledServiceType);

        APIUtils.saveLdapProperties(disabledServicePayload)
            .then(function(response) {
              return APIUtils.saveLdapProperties(enabledServicePayload);
            })
            .then(
                function(response) {
                  if (!response.data.hasOwnProperty('error')) {
                    toastService.success('Successfully updated LDAP settings.');
                    $scope.loadLdap();
                  } else {
                    // The response returned a 200 but there was an error
                    // property sent in the response. It is unclear what
                    // settings were saved. Reloading LDAP to make it clear
                    // to the user.
                    toastService.error('Unable to update all LDAP settings.');
                    $scope.loadLdap();
                    console.log(response.data.error.message);
                  }
                },
                function(error) {
                  toastService.error('Unable to update LDAP settings.');
                  console.log(JSON.stringify(error));
                });
      };

      /**
       * Sends a request to disable the LDAP Service if the user
       * toggled the service enabled checkbox in the UI and if
       * there was a previously saved service type. This prevents
       * unnecessary calls to the backend if the user toggled
       * the service enabled, but never actually had saved settings.
       */
      $scope.updateServiceEnabled = () => {
        const originalEnabledServiceType =
            $scope.originalLdapProperties.EnabledServiceType;

        if (!$scope.ldapProperties.ServiceEnabled &&
            originalEnabledServiceType) {
          $scope.ldapProperties.EnabledServiceType = '';

          const disabledServicePayload =
              createLdapDisableRequest(originalEnabledServiceType);
          APIUtils.saveLdapProperties(disabledServicePayload)
              .then(
                  function(response) {
                    toastService.success('Successfully disabled LDAP.');
                    $scope.roleGroups = [];
                    $scope.loadLdap();
                  },
                  function(error) {
                    toastService.error('Unable to update LDAP settings.');
                    console.log(JSON.stringify(error));
                  });
        }
      };

      /**
       *
       * @param {string} uri for certificate
       * @returns {null | Object}
       */
      function getCertificate(location) {
        return APIUtils.getCertificate(location).then(function(data) {
          if (data.Members && data.Members.length) {
            return APIUtils.getCertificate(data.Members[0]['@odata.id'])
                .then(
                    function(response) {
                      return response;
                    },
                    function(error) {
                      console.log(json.stringify(error));
                    })
          } else {
            return null;
          }
        });
      }

      /**
       *
       * @param {Object} ldapProperties
       * @returns {string}
       */
      function getEnabledServiceType(ldapProperties) {
        let enabledServiceType = '';
        if (ldapProperties.LDAP.ServiceEnabled) {
          enabledServiceType = 'LDAP';
        } else if (ldapProperties.ActiveDirectory.ServiceEnabled) {
          enabledServiceType = 'ActiveDirectory';
        }
        return enabledServiceType;
      }

      /**
       *
       * @param {Object} ldapProperties
       * @returns {Array}
       */
      function getServiceAddresses(ldapProperties) {
        let serviceAddresses = [];
        let serviceType = getEnabledServiceType(ldapProperties);
        if (serviceType) {
          serviceAddresses = ldapProperties[serviceType]['ServiceAddresses'];
        }
        return serviceAddresses;
      }

      /**
       *
       * @param {Object} ldapProperties
       * @returns {boolean}
       */
      function getUseSsl(ldapProperties) {
        let useSsl = false;
        let serviceType = getEnabledServiceType(ldapProperties);
        if (serviceType) {
          const uri = ldapProperties[serviceType]['ServiceAddresses'][0];
          useSsl = uri.startsWith('ldaps://');
        }
        return useSsl;
      }

      /**
       *
       * @param {Object} ldapProperties
       * @returns {string}
       */
      function getUsername(ldapProperties) {
        let username = '';
        let serviceType = getEnabledServiceType(ldapProperties);
        if (serviceType) {
          username = ldapProperties[serviceType]['Authentication']['Username'];
        }
        return username;
      }

      /**
       *
       * @param {Object} ldapProperties
       * @returns {Array}
       */
      function getBaseDistinguishedNames(ldapProperties) {
        let basedDisguishedNames = [];
        let serviceType = getEnabledServiceType(ldapProperties);
        if (serviceType) {
          basedDisguishedNames =
              ldapProperties[serviceType]['LDAPService']['SearchSettings']['BaseDistinguishedNames'];
        }
        return basedDisguishedNames;
      }

      /**
       *
       * @param {Object} ldapProperties
       * @returns {string}
       */
      function getGroupsAttribute(ldapProperties) {
        let groupsAttribute = '';
        let serviceType = getEnabledServiceType(ldapProperties);
        if (serviceType) {
          groupsAttribute =
              ldapProperties[serviceType]['LDAPService']['SearchSettings']['GroupsAttribute'];
        }
        return groupsAttribute;
      }


      /**
       *
       * @param {Object} ldapProperties
       * @returns {string}
       */
      function getUsernameAttribute(ldapProperties) {
        let userNameAttribute = '';
        let serviceType = getEnabledServiceType(ldapProperties);
        if (serviceType) {
          userNameAttribute =
              ldapProperties[serviceType]['LDAPService']['SearchSettings']['UsernameAttribute'];
        }
        return userNameAttribute;
      }

      /**
       *
       * @param {Object} ldapProperties
       * @returns {null | string}
       */
      function getAuthenticationType(ldapProperties) {
        let authenticationType = null;
        let serviceType = getEnabledServiceType(ldapProperties);
        if (serviceType) {
          authenticationType =
              ldapProperties[serviceType]['Authentication']['AuthenticationType'];
        }
        return authenticationType;
      }

      /**
       *
       * @param {Object} ldapProperties
       * @returns {Array} A list of role groups
       */
      function getRoleGroups(ldapProperties) {
        let roleGroups = [];
        let serviceType = getEnabledServiceType(ldapProperties);
        if (serviceType) {
          roleGroups = ldapProperties[serviceType]['RemoteRoleMapping'];
        }

        return roleGroups;
      }

      /**
       * Returns the payload needed to enable an LDAP Service
       * @param {string} serviceType - 'LDAP' or 'ActiveDirectory'
       */
      function createLdapEnableRequest(serviceType, ldapProperties) {
        let ldapRequest = {};
        const ServiceEnabled = true;
        const Authentication = {
          Username: ldapProperties.Username,
          Password: ldapProperties.Password,
          AuthenticationType: ldapProperties.AuthenticationType
        };
        const LDAPService = {
          SearchSettings: {
            BaseDistinguishedNames: ldapProperties.BaseDistinguishedNames,
            GroupsAttribute: ldapProperties.GroupsAttribute,
            UsernameAttribute: ldapProperties.UsernameAttribute
          }
        };
        const ServiceAddresses = ldapProperties.ServiceAddresses;

        if (serviceType == 'LDAP') {
          ldapRequest = {
            LDAP:
                {ServiceEnabled, Authentication, LDAPService, ServiceAddresses}
          };
        } else {
          ldapRequest = {
            ActiveDirectory:
                {ServiceEnabled, Authentication, LDAPService, ServiceAddresses}
          };
        }
        return ldapRequest;
      };

      /**
       * Returns the payload needed to disable an LDAP Service
       * @param {string} serviceType - 'LDAP' or 'ActiveDirectory'
       */
      function createLdapDisableRequest(serviceType) {
        let ldapRequest = {};
        const ServiceEnabled = false;

        if (serviceType == 'LDAP') {
          ldapRequest = {LDAP: {ServiceEnabled}};
        } else {
          ldapRequest = {ActiveDirectory: {ServiceEnabled}};
        }
        return ldapRequest;
      }
    }
  ]);
})(angular);
OpenPOWER on IntegriCloud