Skip to content

Commit

Permalink
Merge branch 'master' into postgres-query-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
svenklemm committed Aug 21, 2018
2 parents 02c5d36 + 97fbf5c commit 1b5486a
Show file tree
Hide file tree
Showing 30 changed files with 608 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ debug.test
/vendor/**/appengine*
*.orig

/devenv/dashboards/bulk-testing/*.json
/devenv/bulk-dashboards/*.json
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* **Prometheus**: Heatmap - fix unhandled error when some points are missing [#12484](https://github.com/grafana/grafana/issues/12484)
* **Prometheus**: Add $__interval, $__interval_ms, $__range, $__range_s & $__range_ms support for dashboard and template queries [#12597](https://github.com/grafana/grafana/issues/12597) [#12882](https://github.com/grafana/grafana/issues/12882), thx [@roidelapluie](https://github.com/roidelapluie)
* **Variables**: Skip unneeded extra query request when de-selecting variable values used for repeated panels [#8186](https://github.com/grafana/grafana/issues/8186), thx [@mtanda](https://github.com/mtanda)
* **Variables**: Limit amount of queries executed when updating variable that other variable(s) are dependent on [#11890](https://github.com/grafana/grafana/issues/11890)
* **Postgres/MySQL/MSSQL**: New $__unixEpochGroup and $__unixEpochGroupAlias macros [#12892](https://github.com/grafana/grafana/issues/12892), thx [@svenklemm](https://github.com/svenklemm)
* **Postgres/MySQL/MSSQL**: Add previous fill mode to $__timeGroup macro which will fill in previously seen value when point is missing [#12756](https://github.com/grafana/grafana/issues/12756), thx [@svenklemm](https://github.com/svenklemm)
* **Postgres/MySQL/MSSQL**: Use floor rounding in $__timeGroup macro function [#12460](https://github.com/grafana/grafana/issues/12460), thx [@svenklemm](https://github.com/svenklemm)
Expand Down
2 changes: 1 addition & 1 deletion devenv/bulk-dashboards/bulk-dashboards.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ providers:
folder: 'Bulk dashboards'
type: file
options:
path: devenv/dashboards/bulk-testing
path: devenv/bulk-dashboards

4 changes: 2 additions & 2 deletions devenv/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ bulkDashboard() {
COUNTER=0
MAX=400
while [ $COUNTER -lt $MAX ]; do
jsonnet -o "dashboards/bulk-testing/dashboard${COUNTER}.json" -e "local bulkDash = import 'dashboards/bulk-testing/bulkdash.jsonnet'; bulkDash + { uid: 'uid-${COUNTER}', title: 'title-${COUNTER}' }"
jsonnet -o "bulk-dashboards/dashboard${COUNTER}.json" -e "local bulkDash = import 'bulk-dashboards/bulkdash.jsonnet'; bulkDash + { uid: 'uid-${COUNTER}', title: 'title-${COUNTER}' }"
let COUNTER=COUNTER+1
done

ln -s -f -r ./dashboards/bulk-testing/bulk-dashboards.yaml ../conf/provisioning/dashboards/custom.yaml
ln -s -f -r ./bulk-dashboards/bulk-dashboards.yaml ../conf/provisioning/dashboards/custom.yaml
}

requiresJsonnet() {
Expand Down
3 changes: 2 additions & 1 deletion docs/sources/alerting/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ There are a couple of configuration options which need to be set up in Grafana U

Once these two properties are set, you can send the alerts to Kafka for further processing or throttling.

### All supported notifier
### All supported notifiers

Name | Type |Support images
-----|------------ | ------
Expand All @@ -148,6 +148,7 @@ Pushover | `pushover` | no
Telegram | `telegram` | no
Line | `line` | no
Prometheus Alertmanager | `prometheus-alertmanager` | no
Microsoft Teams | `teams` | yes



Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"es6-shim": "^0.35.3",
"expect.js": "~0.2.0",
"expose-loader": "^0.7.3",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"file-loader": "^1.1.11",
"fork-ts-checker-webpack-plugin": "^0.4.2",
"gaze": "^1.1.2",
Expand Down Expand Up @@ -63,7 +62,7 @@
"mobx-react-devtools": "^4.2.15",
"mocha": "^4.0.1",
"ng-annotate-loader": "^0.6.1",
"ng-annotate-webpack-plugin": "^0.2.1-pre",
"ng-annotate-webpack-plugin": "^0.3.0",
"ngtemplate-loader": "^2.0.1",
"npm": "^5.4.2",
"optimize-css-assets-webpack-plugin": "^4.0.2",
Expand Down
8 changes: 7 additions & 1 deletion pkg/api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ func tryLoginUsingRememberCookie(c *m.ReqContext) bool {
user := userQuery.Result

// validate remember me cookie
if val, _ := c.GetSuperSecureCookie(user.Rands+user.Password, setting.CookieRememberName); val != user.Login {
signingKey := user.Rands + user.Password
if len(signingKey) < 10 {
c.Logger.Error("Invalid user signingKey")
return false
}

if val, _ := c.GetSuperSecureCookie(signingKey, setting.CookieRememberName); val != user.Login {
return false
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/api/pluginproxy/ds_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,15 @@ func (proxy *DataSourceProxy) applyRoute(req *http.Request) {
SecureJsonData: proxy.ds.SecureJsonData.Decrypt(),
}

routeURL, err := url.Parse(proxy.route.Url)
interpolatedURL, err := interpolateString(proxy.route.Url, data)
if err != nil {
logger.Error("Error parsing plugin route url")
logger.Error("Error interpolating proxy url", "error", err)
return
}

routeURL, err := url.Parse(interpolatedURL)
if err != nil {
logger.Error("Error parsing plugin route url", "error", err)
return
}

Expand Down
21 changes: 20 additions & 1 deletion pkg/api/pluginproxy/ds_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func TestDSRouteRule(t *testing.T) {
{Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
},
},
{
Path: "api/common",
Url: "{{.JsonData.dynamicUrl}}",
Headers: []plugins.AppPluginRouteHeader{
{Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
},
},
},
}

Expand All @@ -57,7 +64,8 @@ func TestDSRouteRule(t *testing.T) {

ds := &m.DataSource{
JsonData: simplejson.NewFromAny(map[string]interface{}{
"clientId": "asd",
"clientId": "asd",
"dynamicUrl": "https://dynamic.grafana.com",
}),
SecureJsonData: map[string][]byte{
"key": key,
Expand All @@ -83,6 +91,17 @@ func TestDSRouteRule(t *testing.T) {
})
})

Convey("When matching route path and has dynamic url", func() {
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/common/some/method")
proxy.route = plugin.Routes[3]
proxy.applyRoute(req)

Convey("should add headers and interpolate the url", func() {
So(req.URL.String(), ShouldEqual, "https://dynamic.grafana.com/some/method")
So(req.Header.Get("x-header"), ShouldEqual, "my secret 123")
})
})

Convey("Validating request", func() {
Convey("plugin route with valid role", func() {
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method")
Expand Down
41 changes: 40 additions & 1 deletion pkg/services/sqlstore/migrations/user_mig.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package migrations

import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
import (
"fmt"

"github.com/go-xorm/xorm"
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/util"
)

func addUserMigrations(mg *Migrator) {
userV1 := Table{
Expand Down Expand Up @@ -107,4 +113,37 @@ func addUserMigrations(mg *Migrator) {
mg.AddMigration("Add last_seen_at column to user", NewAddColumnMigration(userV2, &Column{
Name: "last_seen_at", Type: DB_DateTime, Nullable: true,
}))

// Adds salt & rands for old users who used ldap or oauth
mg.AddMigration("Add missing user data", &AddMissingUserSaltAndRandsMigration{})
}

type AddMissingUserSaltAndRandsMigration struct {
MigrationBase
}

func (m *AddMissingUserSaltAndRandsMigration) Sql(dialect Dialect) string {
return "code migration"
}

type TempUserDTO struct {
Id int64
Login string
}

func (m *AddMissingUserSaltAndRandsMigration) Exec(sess *xorm.Session, mg *Migrator) error {
users := make([]*TempUserDTO, 0)

err := sess.Sql(fmt.Sprintf("SELECT id, login from %s WHERE rands = ''", mg.Dialect.Quote("user"))).Find(&users)
if err != nil {
return err
}

for _, user := range users {
_, err := sess.Exec("UPDATE "+mg.Dialect.Quote("user")+" SET salt = ?, rands = ? WHERE id = ?", util.GetRandomString(10), util.GetRandomString(10), user.Id)
if err != nil {
return err
}
}
return nil
}
16 changes: 11 additions & 5 deletions pkg/services/sqlstore/migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type Migrator struct {
x *xorm.Engine
dialect Dialect
Dialect Dialect
migrations []Migration
Logger log.Logger
}
Expand All @@ -31,7 +31,7 @@ func NewMigrator(engine *xorm.Engine) *Migrator {
mg.x = engine
mg.Logger = log.New("migrator")
mg.migrations = make([]Migration, 0)
mg.dialect = NewDialect(mg.x)
mg.Dialect = NewDialect(mg.x)
return mg
}

Expand Down Expand Up @@ -86,7 +86,7 @@ func (mg *Migrator) Start() error {
continue
}

sql := m.Sql(mg.dialect)
sql := m.Sql(mg.Dialect)

record := MigrationLog{
MigrationId: m.Id(),
Expand Down Expand Up @@ -122,15 +122,21 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {

condition := m.GetCondition()
if condition != nil {
sql, args := condition.Sql(mg.dialect)
sql, args := condition.Sql(mg.Dialect)
results, err := sess.SQL(sql).Query(args...)
if err != nil || len(results) == 0 {
mg.Logger.Debug("Skipping migration condition not fulfilled", "id", m.Id())
return sess.Rollback()
}
}

_, err := sess.Exec(m.Sql(mg.dialect))
var err error
if codeMigration, ok := m.(CodeMigration); ok {
err = codeMigration.Exec(sess, mg)
} else {
_, err = sess.Exec(m.Sql(mg.Dialect))
}

if err != nil {
mg.Logger.Error("Executing migration failed", "id", m.Id(), "error", err)
return err
Expand Down
7 changes: 7 additions & 0 deletions pkg/services/sqlstore/migrator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package migrator
import (
"fmt"
"strings"

"github.com/go-xorm/xorm"
)

const (
Expand All @@ -19,6 +21,11 @@ type Migration interface {
GetCondition() MigrationCondition
}

type CodeMigration interface {
Migration
Exec(sess *xorm.Session, migrator *Migrator) error
}

type SQLType string

type ColumnType string
Expand Down
5 changes: 3 additions & 2 deletions pkg/services/sqlstore/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ func CreateUser(ctx context.Context, cmd *m.CreateUserCommand) error {
LastSeenAt: time.Now().AddDate(-10, 0, 0),
}

user.Salt = util.GetRandomString(10)
user.Rands = util.GetRandomString(10)

if len(cmd.Password) > 0 {
user.Salt = util.GetRandomString(10)
user.Rands = util.GetRandomString(10)
user.Password = util.EncodePassword(cmd.Password, user.Salt)
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/services/sqlstore/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ func TestUserDataAccess(t *testing.T) {
Convey("Testing DB", t, func() {
InitTestDB(t)

Convey("Creating a user", func() {
cmd := &m.CreateUserCommand{
Email: "usertest@test.com",
Name: "user name",
Login: "user_test_login",
}

err := CreateUser(context.Background(), cmd)
So(err, ShouldBeNil)

Convey("Loading a user", func() {
query := m.GetUserByIdQuery{Id: cmd.Result.Id}
err := GetUserById(&query)
So(err, ShouldBeNil)

So(query.Result.Email, ShouldEqual, "usertest@test.com")
So(query.Result.Password, ShouldEqual, "")
So(query.Result.Rands, ShouldHaveLength, 10)
So(query.Result.Salt, ShouldHaveLength, 10)
})
})

Convey("Given 5 users", func() {
var err error
var cmd *m.CreateUserCommand
Expand Down
8 changes: 7 additions & 1 deletion public/app/containers/Explore/Explore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export class Explore extends React.Component<any, IExploreState> {
datasourceError: null,
datasourceLoading: true,
graphResult: null,
latency: 0,
logsResult: null,
queryErrors: [],
queryHints: [],
Expand Down Expand Up @@ -254,7 +255,10 @@ export class Explore extends React.Component<any, IExploreState> {
this.setState({
graphResult: null,
logsResult: null,
latency: 0,
queries: ensureQueries(),
queryErrors: [],
queryHints: [],
tableResult: null,
});
};
Expand All @@ -276,8 +280,10 @@ export class Explore extends React.Component<any, IExploreState> {

onClickSplit = () => {
const { onChangeSplit } = this.props;
const state = { ...this.state };
state.queries = state.queries.map(({ edited, ...rest }) => rest);
if (onChangeSplit) {
onChangeSplit(true, this.state);
onChangeSplit(true, state);
}
};

Expand Down
19 changes: 19 additions & 0 deletions public/app/containers/Explore/PromQueryField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ describe('PromQueryField typeahead handling', () => {
expect(result.suggestions).toEqual([{ items: [{ label: 'bar' }], label: 'Labels' }]);
});

it('returns label suggestions on label context but leaves out labels that already exist', () => {
const instance = shallow(
<PromQueryField {...defaultProps} labelKeys={{ '{job="foo"}': ['bar', 'job'] }} />
).instance() as PromQueryField;
const value = Plain.deserialize('{job="foo",}');
const range = value.selection.merge({
anchorOffset: 11,
});
const valueWithSelection = value.change().select(range).value;
const result = instance.getTypeahead({
text: '',
prefix: '',
wrapperClasses: ['context-labels'],
value: valueWithSelection,
});
expect(result.context).toBe('context-labels');
expect(result.suggestions).toEqual([{ items: [{ label: 'bar' }], label: 'Labels' }]);
});

it('returns a refresher on label context and unavailable metric', () => {
const instance = shallow(
<PromQueryField {...defaultProps} labelKeys={{ '{__name__="foo"}': ['bar'] }} />
Expand Down
Loading

0 comments on commit 1b5486a

Please sign in to comment.