grafana/pkg/services/sqlstore/migrations/apikey_mig.go

104 lines
3.8 KiB
Go
Raw Normal View History

package migrations
import (
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
)
func addApiKeyMigrations(mg *Migrator) {
2015-02-24 10:59:21 -06:00
apiKeyV1 := Table{
Name: "api_key",
Columns: []*Column{
2015-03-07 09:23:22 -06:00
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "account_id", Type: DB_BigInt, Nullable: false},
{Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
2015-03-07 09:23:22 -06:00
{Name: "key", Type: DB_Varchar, Length: 64, Nullable: false},
{Name: "role", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false},
2015-02-24 10:59:21 -06:00
},
Indices: []*Index{
2015-03-07 09:23:22 -06:00
{Cols: []string{"account_id"}},
{Cols: []string{"key"}, Type: UniqueIndex},
{Cols: []string{"account_id", "name"}, Type: UniqueIndex},
2015-02-24 10:59:21 -06:00
},
}
// create table
mg.AddMigration("create api_key table", NewAddTableMigration(apiKeyV1))
// create indices
mg.AddMigration("add index api_key.account_id", NewAddIndexMigration(apiKeyV1, apiKeyV1.Indices[0]))
mg.AddMigration("add index api_key.key", NewAddIndexMigration(apiKeyV1, apiKeyV1.Indices[1]))
mg.AddMigration("add index api_key.account_id_name", NewAddIndexMigration(apiKeyV1, apiKeyV1.Indices[2]))
// ---------------------
// account -> org changes
2015-02-24 10:59:21 -06:00
// drop indexes
addDropAllIndicesMigrations(mg, "v1", apiKeyV1)
// rename table
addTableRenameMigration(mg, "api_key", "api_key_v1", "v1")
apiKeyV2 := Table{
Name: "api_key",
Columns: []*Column{
2015-03-07 09:23:22 -06:00
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "org_id", Type: DB_BigInt, Nullable: false},
{Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
{Name: "key", Type: DB_Varchar, Length: 190, Nullable: false},
2015-03-07 09:23:22 -06:00
{Name: "role", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false},
2015-02-24 10:59:21 -06:00
},
Indices: []*Index{
2015-03-07 09:23:22 -06:00
{Cols: []string{"org_id"}},
{Cols: []string{"key"}, Type: UniqueIndex},
{Cols: []string{"org_id", "name"}, Type: UniqueIndex},
2015-02-24 10:59:21 -06:00
},
}
// create v2 table
mg.AddMigration("create api_key table v2", NewAddTableMigration(apiKeyV2))
// add v2 indíces
addTableIndicesMigrations(mg, "v2", apiKeyV2)
//------- copy data from v1 to v2 -------------------
mg.AddMigration("copy api_key v1 to v2", NewCopyTableDataMigration("api_key", "api_key_v1", map[string]string{
"id": "id",
"org_id": "account_id",
"name": "name",
"key": "key",
"role": "role",
"created": "created",
"updated": "updated",
}))
mg.AddMigration("Drop old table api_key_v1", NewDropTableMigration("api_key_v1"))
mg.AddMigration("Update api_key table charset", NewTableCharsetMigration("api_key", []*Column{
{Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
{Name: "key", Type: DB_Varchar, Length: 190, Nullable: false},
{Name: "role", Type: DB_NVarchar, Length: 255, Nullable: false},
}))
Auth: Allow expiration of API keys (#17678) * Modify backend to allow expiration of API Keys * Add middleware test for expired api keys * Modify frontend to enable expiration of API Keys * Fix frontend tests * Fix migration and add index for `expires` field * Add api key tests for database access * Substitude time.Now() by a mock for test usage * Front-end modifications * Change input label to `Time to live` * Change input behavior to comply with the other similar * Add tooltip * Modify AddApiKey api call response Expiration should be *time.Time instead of string * Present expiration date in the selected timezone * Use kbn for transforming intervals to seconds * Use `assert` library for tests * Frontend fixes Add checks for empty/undefined/null values * Change expires column from datetime to integer * Restrict api key duration input It should be interval not number * AddApiKey must complain if SecondsToLive is negative * Declare ErrInvalidApiKeyExpiration * Move configuration to auth section * Update docs * Eliminate alias for models in modified files * Omit expiration from api response if empty * Eliminate Goconvey from test file * Fix test Do not sleep, use mocked timeNow() instead * Remove index for expires from api_key table The index should be anyway on both org_id and expires fields. However this commit eliminates completely the index for now since not many rows are expected to be in this table. * Use getTimeZone function * Minor change in api key listing The frontend should display a message instead of empty string if the key does not expire.
2019-06-26 01:47:03 -05:00
mg.AddMigration("Add expires to api_key table", NewAddColumnMigration(apiKeyV2, &Column{
Name: "expires", Type: DB_BigInt, Nullable: true,
}))
mg.AddMigration("Add service account foreign key", NewAddColumnMigration(apiKeyV2, &Column{
Name: "service_account_id", Type: DB_BigInt, Nullable: true,
}))
mg.AddMigration("set service account foreign key to nil if 0", NewRawSQLMigration(
"UPDATE api_key SET service_account_id = NULL WHERE service_account_id = 0;"))
mg.AddMigration("Add last_used_at to api_key table", NewAddColumnMigration(apiKeyV2, &Column{
Name: "last_used_at", Type: DB_DateTime, Nullable: true,
}))
// is_revoked indicates whether key is revoked or not. Revoked keys should be kept in the table, but invalid.
mg.AddMigration("Add is_revoked column to api_key table", NewAddColumnMigration(apiKeyV2, &Column{
Name: "is_revoked", Type: DB_Bool, Nullable: true, Default: "0",
}))
}