Skip to content

Commit

Permalink
Ignore SSL option (#38)
Browse files Browse the repository at this point in the history
* Ignore SSL option

ChangeLog:
  - Allows for a config option to ignore invalid SSL certificates.
  - Allows for graceful failover when Orgs cannot be obtained.

Fixes #37 and #35

* Restructuring documentation
  • Loading branch information
safaci2000 authored Jul 29, 2021
1 parent c03561b commit 64e3804
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 67 deletions.
6 changes: 3 additions & 3 deletions api/contract.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

import (
"github.com/netsage-project/grafana-dashboard-manager/config"
"github.com/grafana-tools/sdk"
"github.com/netsage-project/grafana-dashboard-manager/config"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -37,10 +37,10 @@ type DashNGoImpl struct {

func (s *DashNGoImpl) init() {
s.grafanaConf = config.GetDefaultGrafanaConfig()
s.configRef = config.Config()
s.configRef = config.Config().ViperConfig()
s.client = s.Login()
s.adminClient = s.AdminLogin()
s.debug = s.configRef.GetBool("global.debug")
s.debug = config.Config().IsDebug()

}

Expand Down
3 changes: 2 additions & 1 deletion api/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func (s *DashNGoImpl) ListDashboards(filters Filter) []sdk.FoundBoard {
ctx := context.Background()
orgs, err := s.client.GetAllOrgs(ctx)
if err != nil {
log.Fatalf("Error getting organizations: %s", err.Error())
log.Warnf("Error getting organizations: %s", err.Error())
orgs = make([]sdk.Org, 0)
}
if s.grafanaConf.Organization != "" {
var ID uint
Expand Down
22 changes: 20 additions & 2 deletions api/login.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package api

import (
"crypto/tls"
"fmt"
"log"

"net/http"

"github.com/grafana-tools/sdk"
"github.com/netsage-project/grafana-dashboard-manager/config"
log "github.com/sirupsen/logrus"
)

//Login: Logs into grafana returning a client instance using Token or Basic Auth
func (s *DashNGoImpl) Login() *sdk.Client {

//If ignoreSSL create custom http client
if config.Config().IgnoreSSL() {
ignoreSSLErrors()
}
if s.grafanaConf.APIToken != "" {
return s.tokenLogin()
} else if s.grafanaConf.UserName != "" && s.grafanaConf.Password != "" {
Expand All @@ -30,10 +39,19 @@ func (s *DashNGoImpl) AdminLogin() *sdk.Client {

}

//ignoreSSLErrors when called replaces the default http client to ignore invalid SSL issues.
//only to be used for testing, highly discouraged in production.
func ignoreSSLErrors() {
customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
httpclient := &http.Client{Transport: customTransport}
sdk.DefaultHTTPClient = httpclient

}

//tokenLogin: given a URL and token return the client
func (s *DashNGoImpl) tokenLogin() *sdk.Client {
client, err := sdk.NewClient(s.grafanaConf.URL, s.grafanaConf.APIToken, sdk.DefaultHTTPClient)

if err != nil {
log.Fatal("failed to get a valid client using token auth")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
}

func initConfig() {
configProvider := config.Config()
configProvider := config.Config().ViperConfig()
setupGrafanaClient()
log.Debug("Creating output locations")
dir := configProvider.GetString("env.output.datasources")
Expand Down
1 change: 1 addition & 0 deletions conf/importer-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ contexts:

global:
debug: true
ignore_ssl_errors: false ##when set to true will ignore invalid SSL errors
31 changes: 26 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,34 @@ type ConfigStruct struct {

var configData *ConfigStruct

// Config returns a default config providers
func Config() *viper.Viper {
return configData.defaultConfig
//ViperConfig returns the the loaded configuration via a viper reference
func (s *ConfigStruct) ViperConfig() *viper.Viper {
return s.defaultConfig
}

//IsDebug returns true if debug mode is enabled
func (s ConfigStruct) IsDebug() bool {
return s.defaultConfig.GetBool("global.debug")
}

//IgnoreSSL returns true if SSL errors should be ignored
func (s ConfigStruct) IgnoreSSL() bool {
return s.defaultConfig.GetBool("global.ignore_ssl_errors")
}

// func Config
func Config() *ConfigStruct {
return configData
}

//GetContext returns the name of the selected context
func GetContext() string {
name := Config().GetString("context_name")
name := Config().ViperConfig().GetString("context_name")
return name
}

//SetContext will try to find the specified context, if it exists in the file, will re-write the importer.yml
//with the selected context
func SetContext(context string) {
v := LoadConfigProvider("importer")
m := v.GetStringMap(fmt.Sprintf("contexts.%s", context))
Expand All @@ -38,10 +56,12 @@ func SetContext(context string) {
v.WriteConfig()
}

//GetContexts returns all available contexts
func GetContexts() []string {
return funk.Keys(configData.contextMap).([]string)
}

//GetGrafanaConfig returns the selected context or terminates app if not found
func GetGrafanaConfig(name string) *GrafanaConfig {
val, ok := configData.contextMap[name]
if ok {
Expand All @@ -54,6 +74,7 @@ func GetGrafanaConfig(name string) *GrafanaConfig {
return nil
}

//GetDefaultGrafanaConfig returns the default aka. selected grafana config
func GetDefaultGrafanaConfig() *GrafanaConfig {
return GetGrafanaConfig(GetContext())
}
Expand All @@ -78,6 +99,7 @@ func init() {

}

//readViperConfig utilizes the viper library to load the config from the selected paths
func readViperConfig(appName string) *viper.Viper {
v := viper.New()
v.SetEnvPrefix(appName)
Expand All @@ -88,7 +110,6 @@ func readViperConfig(appName string) *viper.Viper {
v.AutomaticEnv()

// global defaults

v.SetDefault("json_logs", false)
v.SetDefault("loglevel", "debug")

Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestSetup(t *testing.T) {
conf := config.Config()
conf := config.Config().ViperConfig()
assert.NotNil(t, conf)
context := conf.GetString("context_name")
assert.Equal(t, context, "qa")
Expand Down
2 changes: 1 addition & 1 deletion documentation/content/docs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ weight: 1
---


{{< button "./usage_guide" "Usage Guide" "mb-1" >}}
{{< button "./configuration" "Configuration" "mb-1" >}}

38 changes: 38 additions & 0 deletions documentation/content/docs/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: "Configuration"
weight: 14
---
## Getting started

This project requires Go to be installed. On OS X with Homebrew you can just run `brew install go`.





make a copy of [conf/importer-example.yml](https://github.com/netsage-project/grafana-dashboard-manager/blob/master/conf/importer-example.yml) and name it `conf/importer.yml` You'll need GRAFANA ADMINISTRATIVE privileges to proceed.


### Authentication


You can use either an Auth Token or username/password credentials. If you configure both then the Token is given priority.

Watched folders under grafana is a white list of folders that are being managed by the tool. By default only "General" is managed.

env.output defines where the files will be saved and imported from.

### Global Flags

`globals.debug` when set will print a more verbose output (Development In Progress)
`globals.ignore_ssl_errors` when set will disregard any SSL errors and proceed as expected


### Building/Running the app

Running it then should be as simple as:

```bash
$ make build
$ ./bin/grafana-dashboard-manager
```
30 changes: 30 additions & 0 deletions documentation/content/docs/developer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: "Developer Guide"
weight: 14
---
## Making a release

Install goreleaser.

```sh
brew install goreleaser/tap/goreleaser
brew reinstall goreleaser`
```

export your GITHUB_TOKEN.

```sh
export GITHUB_TOKEN="secret"
```

git tag v0.1.0
goreleaser release


NOTE: CI/CD pipeline should do all this automatically. `make release-snapshot` is used to test the release build process. Once a build is tagged all artifacts should be built automatically and attached to the github release page.

NOTE: mac binary are not signed so will likely complain.




60 changes: 8 additions & 52 deletions documentation/content/docs/usage_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@
title: "Usage Guide"
weight: 14
---
## Getting started

This project requires Go to be installed. On OS X with Homebrew you can just run `brew install go`.

### Configuring Auth

make a copy of `conf/importer-example.yml` and name it `conf/importer.yml` You'll need administrative privileges to proceed.

You can use either an Auth Token or username/password credentials. If you configure both then the Token is given priority.

Watched folders under grafana is a white list of folders that are being managed by the tool. By default only "General" is managed.

env.output defines where the files will be saved and imported from.
Every namespace supporting CRUD operations has the functions: list, import, export, clear operating on only the monitored folders.

### Contexts

Expand All @@ -28,30 +17,9 @@ ctx is shorthand for context
./bin/grafana-dashboard-manager ctx set -c production -- updates the active config and sets it to the request value.
```

### Users

Only supported with basic auth. Users is the only one where basic auth is given priority. API Auth is not supported, so will try to use basic auth if configured otherwise will warn the user and exit.

```sh
./bin/grafana-dashboard-manager users list -- Lists all known users
./bin/grafana-dashboard-manager users promote -u [email protected] -- promotes the user to a grafana admin
```


### Running the app

Running it then should be as simple as:

```bash
$ make build
$ ./bin/grafana-dashboard-manager
```

Every namespace has three functions: list, import, export, clear operating on only the monitored folders.

#### Dashboards

Dashboards are imported or exported from _organization_ specified in configuration file otherwise current organitazione user is used.
Dashboards are imported or exported from _organization_ specified in configuration file otherwise current organization user is used.

All commands can use `dashboards` or `dash` to manage dashboards

Expand All @@ -64,8 +32,8 @@ All commands can use `dashboards` or `dash` to manage dashboards

#### DataSources

DataSources credentials are keyed by the name of the DataSource. See see [config example](https://github.com/netsage-project/grafana-dashboard-manager/blob/master/conf/importer-example.yml). If the datasource JSON doesn't have auth enabled, the credentials are igored. If Credentials are missing, we'll fall back on default credentials if any exist. The password is set as a value for basicAuthPassword in the API payload.
Datasources are imported or exported from _organization_ specified in configuration file otherwise current organitazione user is used.
DataSources credentials are keyed by the name of the DataSource. See see [config example](https://github.com/netsage-project/grafana-dashboard-manager/blob/master/conf/importer-example.yml). If the datasource JSON doesn't have auth enabled, the credentials are ignored. If Credentials are missing, we'll fall back on default credentials if any exist. The password is set as a value for basicAuthPassword in the API payload.
Datasources are imported or exported from _organization_ specified in configuration file otherwise current organization user is used.


All commands can use `datasources` or `ds` to manage datasources
Expand All @@ -84,24 +52,12 @@ Command can use `organizations` or `org` to manage organizations.
./bin/grafana-dashboard-manager org list -- Lists all organizations
```

## Making a release

Install goreleaser.

```sh
brew install goreleaser/tap/goreleaser
brew reinstall goreleaser`
```
### Users

export your GITHUB_TOKEN.
Only supported with basic auth. Users is the only one where basic auth is given priority. API Auth is not supported, so will try to use basic auth if configured otherwise will warn the user and exit.

```sh
export GITHUB_TOKEN="secret"
./bin/grafana-dashboard-manager users list -- Lists all known users
./bin/grafana-dashboard-manager users promote -u [email protected] -- promotes the user to a grafana admin
```

git tag v0.1.0
goreleaser release




2 changes: 1 addition & 1 deletion integration_tests/integration_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func initTest(t *testing.T) (api.ApiService, *viper.Viper) {
conf := config.Config()
conf := config.Config().ViperConfig()
assert.NotNil(t, conf)
conf.Set("context_name", "testing")
//Hack for Local testing
Expand Down

0 comments on commit 64e3804

Please sign in to comment.