Repository: ambari
Updated Branches:
  refs/heads/branch-2.1 23b7759c5 -> 1c3a06ea3


AMBARI-13508. Kerberos: editing or saving KDC admin creds POST fails


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/1c3a06ea
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/1c3a06ea
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/1c3a06ea

Branch: refs/heads/branch-2.1
Commit: 1c3a06ea3cbc071efe1ea436d5a796914cbb257e
Parents: 594f96b
Author: Alex Antonenko <hiv...@gmail.com>
Authored: Thu Oct 22 16:24:01 2015 +0300
Committer: Alex Antonenko <hiv...@gmail.com>
Committed: Thu Oct 22 16:38:57 2015 +0300

----------------------------------------------------------------------
 ambari-web/app/utils/credentials.js             | 22 ++++--
 .../kdc_credentials_controller_mixin_test.js    | 72 ++++++++++++++------
 2 files changed, 70 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/1c3a06ea/ambari-web/app/utils/credentials.js
----------------------------------------------------------------------
diff --git a/ambari-web/app/utils/credentials.js 
b/ambari-web/app/utils/credentials.js
index cc45288..cf06908 100644
--- a/ambari-web/app/utils/credentials.js
+++ b/ambari-web/app/utils/credentials.js
@@ -87,14 +87,20 @@ module.exports = {
   createOrUpdateCredentials: function(clusterName, alias, resource) {
     var self = this;
     var dfd = $.Deferred();
-    this.createCredentials(clusterName, alias, resource).then(function() {
-      dfd.resolve();
-    }, function() {
+    this.getCredential(clusterName, alias).then(function() {
+      // update previously stored credentials
       self.updateCredentials(clusterName, alias, resource).always(function() {
         var status = arguments[1];
         var result = arguments[2];
         dfd.resolve(status === "success", result);
       });
+    }, function() {
+      // create credentials if they not exist
+      self.createCredentials(clusterName, alias, resource).always(function() {
+        var status = arguments[1];
+        var result = arguments[2];
+        dfd.resolve(status === "success", result);
+      });
     });
     return dfd.promise();
   },
@@ -105,6 +111,7 @@ module.exports = {
    * @member utils.credentials
    * @param {string} clusterName cluster name
    * @param {string} alias credential alias name e.g. "kdc.admin.credentials"
+   * @param {function} [callback] success callback to invoke, credential will 
be passed to first argument
    * @returns {$.Deferred} promise object
    */
   getCredential: function(clusterName, alias, callback) {
@@ -116,14 +123,19 @@ module.exports = {
         alias: alias,
         callback: callback
       },
-      success: 'getCredentialSuccessCallback'
+      success: 'getCredentialSuccessCallback',
+      error: 'getCredentialErrorCallback'
     });
   },
 
   getCredentialSuccessCallback: function(data, opt, params) {
-    params.callback(Em.getWithDefault(data, 'Credential', null));
+    if (params.callback) {
+      params.callback(Em.getWithDefault(data, 'Credential', null));
+    }
   },
 
+  getCredentialErrorCallback: function() {},
+
   /**
    * Update credential by alias and cluster name
    *

http://git-wip-us.apache.org/repos/asf/ambari/blob/1c3a06ea/ambari-web/test/mixins/common/kdc_credentials_controller_mixin_test.js
----------------------------------------------------------------------
diff --git 
a/ambari-web/test/mixins/common/kdc_credentials_controller_mixin_test.js 
b/ambari-web/test/mixins/common/kdc_credentials_controller_mixin_test.js
index 2553397..9f05cef 100644
--- a/ambari-web/test/mixins/common/kdc_credentials_controller_mixin_test.js
+++ b/ambari-web/test/mixins/common/kdc_credentials_controller_mixin_test.js
@@ -76,6 +76,12 @@ describe('App.KDCCredentialsControllerMixin', function() {
         value: value
       });
     };
+    var resolveWith = function(data) {
+      return $.Deferred().resolve(data).promise();
+    };
+    var rejectWith = function(data) {
+      return $.Deferred().reject(data).promise();
+    };
     [
       {
         configs: [
@@ -83,6 +89,9 @@ describe('App.KDCCredentialsControllerMixin', function() {
           createConfig('admin_principal', 'admin/admin'),
           createConfig('persist_credentials', 'true')
         ],
+        credentialsExists: false,
+        createCredentialFnCalled: true,
+        updateCredentialFnCalled: false,
         e: [
           'testName',
           'kdc.admin.credential',
@@ -92,7 +101,27 @@ describe('App.KDCCredentialsControllerMixin', function() {
             principal: 'admin/admin'
           }
         ],
-        message: 'Save Admin credentials checkbox checked, credentials should 
be saved as `persisted`'
+        message: 'Save Admin credentials checkbox checked, credentials already 
stored and should be updated as `persisted`'
+      },
+      {
+        configs: [
+          createConfig('admin_password', 'admin'),
+          createConfig('admin_principal', 'admin/admin'),
+          createConfig('persist_credentials', 'true')
+        ],
+        credentialsExists: true,
+        createCredentialFnCalled: false,
+        updateCredentialFnCalled: true,
+        e: [
+          'testName',
+          'kdc.admin.credential',
+          {
+            type: 'persisted',
+            key: 'admin',
+            principal: 'admin/admin'
+          }
+        ],
+        message: 'Save Admin credentials checkbox checked, no stored 
credentials, should be created as `persisted`'
       },
       {
         configs: [
@@ -100,6 +129,9 @@ describe('App.KDCCredentialsControllerMixin', function() {
           createConfig('admin_principal', 'admin/admin'),
           createConfig('persist_credentials', 'false')
         ],
+        credentialsExists: true,
+        createCredentialFnCalled: false,
+        updateCredentialFnCalled: true,
         e: [
           'testName',
           'kdc.admin.credential',
@@ -109,7 +141,7 @@ describe('App.KDCCredentialsControllerMixin', function() {
             principal: 'admin/admin'
           }
         ],
-        message: 'Save Admin credentials checkbox un-checked, credentials 
should be saved as `temporary`'
+        message: 'Save Admin credentials checkbox unchecked, credentials 
already stored and should be updated as `temporary`'
       },
       {
         configs: [
@@ -117,6 +149,9 @@ describe('App.KDCCredentialsControllerMixin', function() {
           createConfig('admin_principal', 'admin/admin'),
           createConfig('persist_credentials', 'false')
         ],
+        credentialsExists: false,
+        createCredentialFnCalled: true,
+        updateCredentialFnCalled: false,
         e: [
           'testName',
           'kdc.admin.credential',
@@ -126,24 +161,20 @@ describe('App.KDCCredentialsControllerMixin', function() {
             principal: 'admin/admin'
           }
         ],
-        credentialWasSaved: true,
-        message: 'Save Admin credentials checkbox checked, credential was 
saved, credentials should be saved as `temporary`, #updateKDCCredentials should 
be called'
+        message: 'Save Admin credentials checkbox unchecked, credentials 
already stored and should be updated as `temporary`'
       }
     ].forEach(function(test) {
       it(test.message, function() {
         sinon.stub(App, 'get').withArgs('clusterName').returns('testName');
+        sinon.stub(credentialsUtils, 'getCredential', function(clusterName, 
alias) {
+          return test.credentialsExists ? resolveWith() : rejectWith();
+        });
         sinon.stub(credentialsUtils, 'createCredentials', function() {
-          if (test.credentialWasSaved) {
-            return $.Deferred().reject().promise();
-          } else {
-            return $.Deferred().resolve().promise();
-          }
+          return resolveWith();
+        });
+        sinon.stub(credentialsUtils, 'updateCredentials', function() {
+          return resolveWith();
         });
-        if (test.credentialWasSaved) {
-          sinon.stub(credentialsUtils, 'updateCredentials', function() {
-            return $.Deferred().resolve().promise();
-          });
-        }
 
         mixedObject.reopen({
           isStorePersisted: function() {
@@ -151,14 +182,17 @@ describe('App.KDCCredentialsControllerMixin', function() {
           }.property()
         });
         mixedObject.createKDCCredentials(test.configs);
-        assert.isTrue(credentialsUtils.createCredentials.calledOnce, 
'credentialsUtils#createCredentials called');
-        assert.deepEqual(credentialsUtils.createCredentials.args[0], test.e, 
'credentialsUtils#createCredentials called with correct arguments');
+        assert.equal(credentialsUtils.createCredentials.calledOnce, 
test.createCredentialFnCalled,  'credentialsUtils#createCredentials called');
+        if (test.createCredentialFnCalled) {
+          assert.deepEqual(credentialsUtils.createCredentials.args[0], test.e, 
'credentialsUtils#createCredentials called with correct arguments');
+        }
         credentialsUtils.createCredentials.restore();
-        if (test.credentialWasSaved) {
-          assert.isTrue(credentialsUtils.updateCredentials.calledOnce, 
'credentialUtils#updateCredentials called');
+        assert.equal(credentialsUtils.updateCredentials.calledOnce, 
test.updateCredentialFnCalled, 'credentialUtils#updateCredentials called');
+        if (test.updateCredentialFnCalled) {
           assert.deepEqual(credentialsUtils.updateCredentials.args[0], test.e, 
'credentialUtils#updateCredentials called with correct arguments');
-          credentialsUtils.updateCredentials.restore();
         }
+        credentialsUtils.updateCredentials.restore();
+        credentialsUtils.getCredential.restore();
         App.get.restore();
       });
     });

Reply via email to