2020-10-19 12:11:25 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
"storj.io/storj/private/multinodeauth"
|
2020-11-09 15:22:30 +00:00
|
|
|
"storj.io/storj/storagenode/apikeys"
|
2020-10-19 12:11:25 +01:00
|
|
|
)
|
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
// ensures that apiKeysDB implements apikeys.DB interface.
|
|
|
|
var _ apikeys.DB = (*apiKeysDB)(nil)
|
2020-10-19 12:11:25 +01:00
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
// ErrAPIKeysDB represents errors from the api keys database.
|
2021-04-28 09:06:17 +01:00
|
|
|
var ErrAPIKeysDB = errs.Class("apikeysdb")
|
2020-10-19 12:11:25 +01:00
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
// APIKeysDBName represents the database name.
|
|
|
|
const APIKeysDBName = "secret"
|
2020-10-19 12:11:25 +01:00
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
// apiKeysDB works with node api keys DB.
|
|
|
|
type apiKeysDB struct {
|
2020-10-19 12:11:25 +01:00
|
|
|
dbContainerImpl
|
|
|
|
}
|
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
// Store stores api key into database.
|
|
|
|
func (db *apiKeysDB) Store(ctx context.Context, apiKey apikeys.APIKey) (err error) {
|
2020-10-19 12:11:25 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
query := `INSERT INTO secret (
|
|
|
|
token,
|
|
|
|
created_at
|
|
|
|
) VALUES(?,?)`
|
|
|
|
|
|
|
|
_, err = db.ExecContext(ctx, query,
|
2020-12-26 01:16:43 +00:00
|
|
|
apiKey.Secret[:],
|
|
|
|
apiKey.CreatedAt,
|
2020-10-19 12:11:25 +01:00
|
|
|
)
|
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
return ErrAPIKeysDB.Wrap(err)
|
2020-10-19 12:11:25 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
// Check checks if api key exists in db by secret.
|
|
|
|
func (db *apiKeysDB) Check(ctx context.Context, secret multinodeauth.Secret) (err error) {
|
2020-10-19 12:11:25 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var bytes []uint8
|
|
|
|
var createdAt string
|
|
|
|
|
|
|
|
rowStub := db.QueryRowContext(ctx,
|
|
|
|
`SELECT token, created_at FROM secret WHERE token = ?`,
|
2020-12-26 01:16:43 +00:00
|
|
|
secret[:],
|
2020-10-19 12:11:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
err = rowStub.Scan(
|
|
|
|
&bytes,
|
|
|
|
&createdAt,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2020-12-26 01:16:43 +00:00
|
|
|
return apikeys.ErrNoAPIKey.Wrap(err)
|
2020-10-19 12:11:25 +01:00
|
|
|
}
|
2020-12-26 01:16:43 +00:00
|
|
|
return ErrAPIKeysDB.Wrap(err)
|
2020-10-19 12:11:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
// Revoke removes api key from db.
|
|
|
|
func (db *apiKeysDB) Revoke(ctx context.Context, secret multinodeauth.Secret) (err error) {
|
2020-10-19 12:11:25 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
query := `DELETE FROM secret WHERE token = ?`
|
|
|
|
|
|
|
|
_, err = db.ExecContext(ctx, query, secret[:])
|
|
|
|
|
2020-12-26 01:16:43 +00:00
|
|
|
return ErrAPIKeysDB.Wrap(err)
|
2020-10-19 12:11:25 +01:00
|
|
|
}
|