Просмотр исходного кода

basic authentication added for elasticsearch

oroce 12 лет назад
Родитель
Сommit
a56a2b0057

+ 12 - 9
src/app/components/settings.js

@@ -28,16 +28,19 @@ function (_, crypto) {
       settings[key] = typeof options[key] !== 'undefined' ? options[key]  : defaults[key];
     });
 
-    var url = settings.graphiteUrl;
-    var passwordAt = url.indexOf('@');
-    if (passwordAt > 0) {
-      var userStart = url.indexOf('//') + 2;
-      var userAndPassword = url.substring(userStart, passwordAt);
-      var bytes = crypto.charenc.Binary.stringToBytes(userAndPassword);
-      var base64 = crypto.util.bytesToBase64(bytes);
-      settings.graphiteBasicAuth = base64;
-    }
+    var basicAuth = function(url) {
+      var passwordAt = url.indexOf('@');
+      if (passwordAt > 0) {
+        var userStart = url.indexOf('//') + 2;
+        var userAndPassword = url.substring(userStart, passwordAt);
+        var bytes = crypto.charenc.Binary.stringToBytes(userAndPassword);
+        var base64 = crypto.util.bytesToBase64(bytes);
+        return base64;
+      }
+    };
 
+    settings.graphiteBasicAuth = basicAuth(settings.graphiteUrl);
+    settings.elasticsearchBasicAuth = basicAuth(settings.elasticsearch);
     return settings;
   };
 });

+ 1 - 1
src/app/controllers/dash.js

@@ -58,7 +58,7 @@ function (angular, $, config, _) {
 
       $scope.reset_row();
 
-      $scope.ejs = ejsResource(config.elasticsearch);
+      $scope.ejs = ejsResource(config.elasticsearch, config.elasticsearchBasicAuth);
 
       $scope.bindKeyboardShortcuts();
     };

+ 9 - 3
src/app/controllers/metricKeys.js

@@ -10,7 +10,13 @@ function (angular, _, config) {
 
   module.controller('MetricKeysCtrl', function($scope, $http, $q) {
     var elasticSearchUrlForMetricIndex = config.elasticsearch + '/' + config.grafana_metrics_index + '/';
-
+    var httpOptions = {};
+    if (config.elasticsearchBasicAuth) {
+      options.withCredentials = true;
+      options.headers = {
+        "Authorization": "Basic " + config.elasticsearchBasicAuth
+      };
+    }
     $scope.init = function () {
       $scope.metricPath = "prod.apps.api.boobarella.*";
       $scope.metricCounter = 0;
@@ -77,7 +83,7 @@ function (angular, _, config) {
     function deleteIndex()
     {
       var deferred = $q.defer();
-      $http.delete(elasticSearchUrlForMetricIndex)
+      $http.delete(elasticSearchUrlForMetricIndex, httpOptions)
         .success(function() {
           deferred.resolve('ok');
         })
@@ -124,7 +130,7 @@ function (angular, _, config) {
             }
           }
         }
-      });
+      }, httpOptions);
     }
 
     function receiveMetric(result) {

+ 11 - 3
src/app/services/dashboard.js

@@ -47,7 +47,7 @@ function (angular, $, kbn, _, config, moment, Modernizr) {
     };
 
     // An elasticJS client to use
-    var ejs = ejsResource(config.elasticsearch);
+    var ejs = ejsResource(config.elasticsearch, config.elasticsearchBasicAuth);
     var gist_pattern = /(^\d{5,}$)|(^[a-z0-9]{10,}$)|(gist.github.com(\/*.*)\/[a-z0-9]{5,}\/*$)/;
 
     // Store a reference to this
@@ -286,13 +286,21 @@ function (angular, $, kbn, _, config, moment, Modernizr) {
     };
 
     this.elasticsearch_load = function(type,id) {
-      return $http({
+      var options = {
         url: config.elasticsearch + "/" + config.grafana_index + "/"+type+"/"+id+'?' + new Date().getTime(),
         method: "GET",
         transformResponse: function(response) {
           return renderTemplate(angular.fromJson(response)._source.dashboard, $routeParams);
         }
-      }).error(function(data, status) {
+      };
+      if (config.elasticsearchBasicAuth) {
+        options.withCredentials = true;
+        options.headers = {
+          "Authorization": "Basic " + config.elasticsearchBasicAuth
+        };
+      }
+      return $http(options)
+      .error(function(data, status) {
         if(status === 0) {
           alertSrv.set('Error',"Could not contact Elasticsearch at "+config.elasticsearch+
             ". Please ensure that Elasticsearch is reachable from your system." ,'error');

+ 7 - 1
src/vendor/elasticjs/elastic-angular-client.js

@@ -13,7 +13,7 @@ be injected into your angular controllers.
 angular.module('elasticjs.service', [])
   .factory('ejsResource', ['$http', function ($http) {
 
-  return function (config) {
+  return function (config, basicAuth) {
 
     var
 
@@ -43,6 +43,12 @@ angular.module('elasticjs.service', [])
       config.server = '';
     }
 
+    // set authentication header
+    if (basicAuth || config.basicAuth) {
+      config.headers = angular.extend( config.headers||{}, {
+        "Authorization": "Basic " + (basicAuth||config.basicAuth)
+      });
+    }
     /* implement the elastic.js client interface for angular */
     ejs.client = {
       server: function (s) {