예제 #1
0
func NewTokenClient(datasource *models.DataSource) *TokenClient {
	httpClient, _ := datasource.GetHttpClient()

	return &TokenClient{
		log:        log.New("tsdb.mqe.tokenclient"),
		Datasource: datasource,
		HttpClient: httpClient,
	}
}
예제 #2
0
파일: prometheus.go 프로젝트: CamJN/grafana
func NewPrometheusExecutor(dsInfo *models.DataSource) (tsdb.Executor, error) {
	transport, err := dsInfo.GetHttpTransport()
	if err != nil {
		return nil, err
	}

	return &PrometheusExecutor{
		DataSource: dsInfo,
		Transport:  transport,
	}, nil
}
예제 #3
0
func NewOpenTsdbExecutor(datasource *models.DataSource) (tsdb.Executor, error) {
	httpClient, err := datasource.GetHttpClient()

	if err != nil {
		return nil, err
	}

	return &OpenTsdbExecutor{
		DataSource: datasource,
		httpClient: httpClient,
	}, nil
}
예제 #4
0
func TestDataSourceProxy(t *testing.T) {
	Convey("When getting graphite datasource proxy", t, func() {
		ds := m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE}
		targetUrl, err := url.Parse(ds.Url)
		proxy := NewReverseProxy(&ds, "/render", targetUrl)
		proxy.Transport, err = ds.GetHttpTransport()
		So(err, ShouldBeNil)

		transport, ok := proxy.Transport.(*http.Transport)
		So(ok, ShouldBeTrue)
		So(transport.TLSClientConfig.InsecureSkipVerify, ShouldBeTrue)

		requestUrl, _ := url.Parse("http://grafana.com/sub")
		req := http.Request{URL: requestUrl}

		proxy.Director(&req)

		Convey("Can translate request url and path", func() {
			So(req.URL.Host, ShouldEqual, "graphite:8080")
			So(req.URL.Path, ShouldEqual, "/render")
		})
	})

	Convey("When getting influxdb datasource proxy", t, func() {
		ds := m.DataSource{
			Type:     m.DS_INFLUXDB_08,
			Url:      "http://influxdb:8083",
			Database: "site",
			User:     "******",
			Password: "******",
		}

		targetUrl, _ := url.Parse(ds.Url)
		proxy := NewReverseProxy(&ds, "", targetUrl)

		requestUrl, _ := url.Parse("http://grafana.com/sub")
		req := http.Request{URL: requestUrl}

		proxy.Director(&req)

		Convey("Should add db to url", func() {
			So(req.URL.Path, ShouldEqual, "/db/site/")
		})

		Convey("Should add username and password", func() {
			queryVals := req.URL.Query()
			So(queryVals["u"][0], ShouldEqual, "user")
			So(queryVals["p"][0], ShouldEqual, "password")
		})
	})
}
예제 #5
0
파일: influxdb.go 프로젝트: CamJN/grafana
func NewInfluxDBExecutor(datasource *models.DataSource) (tsdb.Executor, error) {
	httpClient, err := datasource.GetHttpClient()

	if err != nil {
		return nil, err
	}

	return &InfluxDBExecutor{
		DataSource:     datasource,
		QueryParser:    &InfluxdbQueryParser{},
		ResponseParser: &ResponseParser{},
		HttpClient:     httpClient,
	}, nil
}
예제 #6
0
파일: mqe.go 프로젝트: mapr/grafana
func NewMQEExecutor(dsInfo *models.DataSource) (tsdb.Executor, error) {
	httpclient, err := dsInfo.GetHttpClient()
	if err != nil {
		return nil, err
	}

	return &MQEExecutor{
		DataSource:  dsInfo,
		httpClient:  httpclient,
		log:         log.New("tsdb.mqe"),
		queryParser: NewQueryParser(),
		apiClient:   NewApiClient(httpclient, dsInfo),
		tokenClient: NewTokenClient(dsInfo),
	}, nil
}
예제 #7
0
파일: dataproxy.go 프로젝트: whlinw/grafana
/**
 * @function:		func NewReverseProxy(ds *m.DataSource, proxyPath string) *httputil.ReverseProxy
 * @description:	This function initializes a reverse proxy.
 * @related issues:	OWL-028, OWL-017, OWL-002
 * @param:			*m.DataSource ds
 * @param:			string proxyPath
 * @return:			*httputil.ReverseProxy
 * @author:			Don Hsieh
 * @since:			07/17/2015
 * @last modified: 	08/04/2015
 * @called by:		func ProxyDataSourceRequest(c *middleware.Context)
 *					 in pkg/api/dataproxy.go
 */
func NewReverseProxy(ds *m.DataSource, proxyPath string) *httputil.ReverseProxy {
	target, _ := url.Parse(ds.Url)
	director := func(req *http.Request) {
		req.URL.Scheme = target.Scheme
		req.URL.Host = target.Host
		req.Host = target.Host
		reqQueryVals := req.URL.Query()

		if ds.Type == m.DS_INFLUXDB_08 {
			req.URL.Path = util.JoinUrlFragments(target.Path, "db/"+ds.Database+"/"+proxyPath)
			reqQueryVals.Add("u", ds.User)
			reqQueryVals.Add("p", ds.Password)
			req.URL.RawQuery = reqQueryVals.Encode()
		} else if ds.Type == m.DS_INFLUXDB {
			req.URL.Path = util.JoinUrlFragments(target.Path, proxyPath)
			reqQueryVals.Add("db", ds.Database)
			reqQueryVals.Add("u", ds.User)
			reqQueryVals.Add("p", ds.Password)
			req.URL.RawQuery = reqQueryVals.Encode()
		} else if ds.Type == "openfalcon" {
			// fmt.Printf("Welcome to %v!\n", ds.Type)
			// fmt.Printf("NewReverseProxy req.URL = %v\n", req.URL)
			reqQueryVals.Add("target", ds.Url)
			req.URL.RawQuery = reqQueryVals.Encode()

			ds.Url = "http://localhost"
			var port = "4001"
			ds.Url += ":" + port
			// fmt.Printf("NewReverseProxy ds.Url = %v\n", ds.Url)
			proxyPath = "/"
			target, _ := url.Parse(ds.Url)
			req.URL.Scheme = target.Scheme
			req.URL.Host = target.Host
			req.Host = target.Host
			req.URL.Path = util.JoinUrlFragments(target.Path, proxyPath)
		} else {
			req.URL.Path = util.JoinUrlFragments(target.Path, proxyPath)
		}
		if ds.BasicAuth {
			req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword))
		}
	}

	return &httputil.ReverseProxy{Director: director}
}
예제 #8
0
func TestDataSourceProxy(t *testing.T) {

	Convey("When getting graphite datasource proxy", t, func() {
		clearCache()
		ds := m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE}
		targetUrl, err := url.Parse(ds.Url)
		proxy := NewReverseProxy(&ds, "/render", targetUrl)
		proxy.Transport, err = DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		transport, ok := proxy.Transport.(*http.Transport)
		So(ok, ShouldBeTrue)
		So(transport.TLSClientConfig.InsecureSkipVerify, ShouldBeTrue)

		requestUrl, _ := url.Parse("http://grafana.com/sub")
		req := http.Request{URL: requestUrl}

		proxy.Director(&req)

		Convey("Can translate request url and path", func() {
			So(req.URL.Host, ShouldEqual, "graphite:8080")
			So(req.URL.Path, ShouldEqual, "/render")
		})
	})

	Convey("When getting influxdb datasource proxy", t, func() {
		clearCache()
		ds := m.DataSource{
			Type:     m.DS_INFLUXDB_08,
			Url:      "http://influxdb:8083",
			Database: "site",
			User:     "******",
			Password: "******",
		}

		targetUrl, _ := url.Parse(ds.Url)
		proxy := NewReverseProxy(&ds, "", targetUrl)

		requestUrl, _ := url.Parse("http://grafana.com/sub")
		req := http.Request{URL: requestUrl}

		proxy.Director(&req)

		Convey("Should add db to url", func() {
			So(req.URL.Path, ShouldEqual, "/db/site/")
		})

		Convey("Should add username and password", func() {
			queryVals := req.URL.Query()
			So(queryVals["u"][0], ShouldEqual, "user")
			So(queryVals["p"][0], ShouldEqual, "password")
		})
	})

	Convey("When caching a datasource proxy", t, func() {
		clearCache()
		ds := m.DataSource{
			Id:   1,
			Url:  "http://k8s:8001",
			Type: "Kubernetes",
		}

		t1, err := DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		t2, err := DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		Convey("Should be using the cached proxy", func() {
			So(t2, ShouldEqual, t1)
		})
	})

	Convey("When getting kubernetes datasource proxy", t, func() {
		clearCache()
		setting.SecretKey = "password"

		json := simplejson.New()
		json.Set("tlsAuth", true)
		json.Set("tlsAuthWithCACert", true)

		t := time.Now()
		ds := m.DataSource{
			Url:     "http://k8s:8001",
			Type:    "Kubernetes",
			Updated: t.Add(-2 * time.Minute),
		}

		transport, err := DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		Convey("Should have no cert", func() {
			So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, true)
		})

		ds.JsonData = json
		ds.SecureJsonData = map[string][]byte{
			"tlsCACert":     util.Encrypt([]byte(caCert), "password"),
			"tlsClientCert": util.Encrypt([]byte(clientCert), "password"),
			"tlsClientKey":  util.Encrypt([]byte(clientKey), "password"),
		}
		ds.Updated = t.Add(-1 * time.Minute)

		transport, err = DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		Convey("Should add cert", func() {
			So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, false)
			So(len(transport.TLSClientConfig.Certificates), ShouldEqual, 1)
		})

		ds.JsonData = nil
		ds.SecureJsonData = map[string][]byte{}
		ds.Updated = t

		transport, err = DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		Convey("Should remove cert", func() {
			So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, true)
			So(len(transport.TLSClientConfig.Certificates), ShouldEqual, 0)
		})
	})

}