Skip to content

Commit

Permalink
refactor module names
Browse files Browse the repository at this point in the history
  • Loading branch information
tobikris committed Oct 5, 2022
1 parent d2e30ca commit dbd46fd
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 32 deletions.
4 changes: 2 additions & 2 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func Authenticate(req *http.Request, s *terraform.State) (ok bool, err error) {

var authenticator Authenticator
switch backend {
case "basic":
case basic.Name:
viper.SetDefault("auth_basic_enabled", true)
if !viper.GetBool("auth_basic_enabled") {
return false, fmt.Errorf("basic auth is not enabled")
}
authenticator = basic.NewBasicAuth()
case "jwt":
case jwt.Name:
issuerURL := viper.GetString("auth_jwt_oidc_issuer_url")
if addr := viper.GetString("vault_addr"); issuerURL != "" && addr != "" {
issuerURL = fmt.Sprintf("%s/v1/identity/oidc", addr)
Expand Down
4 changes: 3 additions & 1 deletion pkg/auth/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"github.com/nimbolus/terraform-backend/pkg/terraform"
)

const Name = "basic"

type BasicAuth struct{}

func NewBasicAuth() *BasicAuth {
return &BasicAuth{}
}

func (l *BasicAuth) GetName() string {
return "basic"
return Name
}

func (b *BasicAuth) Authenticate(secret string, s *terraform.State) (bool, error) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/auth/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/nimbolus/terraform-backend/pkg/terraform"
)

const Name = "jwt"

type JWTAuth struct {
issuerURL string
}
Expand All @@ -19,7 +21,7 @@ func NewJWTAuth(issuerURL string) *JWTAuth {
}

func (l *JWTAuth) GetName() string {
return "jwt"
return Name
}

func (b *JWTAuth) Authenticate(secret string, s *terraform.State) (bool, error) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/kms/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"io"
)

const Name = "local"

type KMS struct {
cipher cipher.AEAD
}
Expand All @@ -34,7 +36,7 @@ func GenerateKey() (string, error) {
}

func (v *KMS) GetName() string {
return "local"
return Name
}

func (s *KMS) Encrypt(d []byte) ([]byte, error) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/kms/transit/transit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
vaultclient "github.com/nimbolus/terraform-backend/pkg/client/vault"
)

const Name = "transit"

type VaultTransit struct {
engine string
key string
Expand All @@ -25,7 +27,7 @@ func NewVaultTransit(engine string, key string) (*VaultTransit, error) {
}

func (v *VaultTransit) GetName() string {
return "transit"
return Name
}

func (v *VaultTransit) Encrypt(d []byte) ([]byte, error) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/lock/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/nimbolus/terraform-backend/pkg/terraform"
)

const Name = "local"

type Lock struct {
mutex sync.Mutex
db map[string][]byte
Expand All @@ -18,7 +20,7 @@ func NewLock() *Lock {
}

func (l *Lock) GetName() string {
return "local"
return Name
}

func (l *Lock) Lock(s *terraform.State) (bool, error) {
Expand Down
25 changes: 14 additions & 11 deletions pkg/lock/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package postgres
import (
"context"
"database/sql"
"fmt"
"time"

pgclient "github.com/nimbolus/terraform-backend/pkg/client/postgres"
"github.com/nimbolus/terraform-backend/pkg/terraform"
)

const Name = "postgres"

type Lock struct {
db *pgclient.Client
}
Expand All @@ -26,7 +27,7 @@ func NewLock() (*Lock, error) {
}

func (l *Lock) GetName() string {
return "pg"
return Name
}

func (l *Lock) Lock(s *terraform.State) (bool, error) {
Expand All @@ -40,9 +41,9 @@ func (l *Lock) Lock(s *terraform.State) (bool, error) {

defer tx.Rollback()

var lockData []byte
var lock []byte

if err := tx.QueryRow(`SELECT lock_data FROM `+l.db.GetLocksTableName()+` WHERE state_id = $1`, s.ID).Scan(&lockData); err != nil {
if err := tx.QueryRow(`SELECT lock_data FROM `+l.db.GetLocksTableName()+` WHERE state_id = $1`, s.ID).Scan(&lock); err != nil {
if err == sql.ErrNoRows {
if _, err := tx.Exec(`INSERT INTO locks (state_id, lock_data) VALUES ($1, $2)`, s.ID, s.Lock); err != nil {
return false, err
Expand All @@ -58,12 +59,14 @@ func (l *Lock) Lock(s *terraform.State) (bool, error) {
return false, err
}

if string(lockData) == string(s.Lock) {
if string(lock) == string(s.Lock) {
// you already have the lock
return true, nil
}

return false, fmt.Errorf("lock already taken for id %s: %s", s.ID, string(lockData))
s.Lock = lock

return false, nil
}

func (l *Lock) Unlock(s *terraform.State) (bool, error) {
Expand All @@ -77,18 +80,18 @@ func (l *Lock) Unlock(s *terraform.State) (bool, error) {

defer tx.Rollback()

var lockData []byte
var lock []byte

if err := tx.QueryRow(`SELECT lock_data FROM `+l.db.GetLocksTableName()+` WHERE state_id = $1`, s.ID).Scan(&lockData); err != nil {
if err := tx.QueryRow(`SELECT lock_data FROM `+l.db.GetLocksTableName()+` WHERE state_id = $1`, s.ID).Scan(&lock); err != nil {
if err == sql.ErrNoRows {
return false, fmt.Errorf("no lock for id %s found", s.ID)
return false, nil
}

return false, err
}

if string(lockData) != string(s.Lock) {
return false, fmt.Errorf("lock mismatch for id %s", s.ID)
if string(lock) != string(s.Lock) {
return false, nil
}

if _, err := tx.Exec(`DELETE FROM `+l.db.GetLocksTableName()+` WHERE state_id = $1 AND lock_data = $2`, s.ID, s.Lock); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/lock/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ package postgres
import (
"testing"

"github.com/spf13/viper"

"github.com/nimbolus/terraform-backend/pkg/lock/util"
)

func init() {
viper.AutomaticEnv()
}

func TestLock(t *testing.T) {
l, err := NewLock()
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions pkg/lock/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import (
"github.com/nimbolus/terraform-backend/pkg/terraform"
)

const lockKey = "terraform-backend-state-lock"
const (
Name = "redis"
lockKey = "terraform-backend-state-lock"
)

type Lock struct {
pool *redigo.Pool
Expand All @@ -39,7 +42,7 @@ func NewLock() *Lock {
}

func (r *Lock) GetName() string {
return "redis"
return Name
}

func (r *Lock) Lock(s *terraform.State) (locked bool, err error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/kms.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

func GetKMS() (k kms.KMS, err error) {
viper.SetDefault("kms_backend", "local")
viper.SetDefault("kms_backend", local.Name)
backend := viper.GetString("kms_backend")

switch backend {
case "local":
case local.Name:
key := viper.GetString("kms_key")
if key == "" {
key, _ = local.GenerateKey()
Expand All @@ -38,7 +38,7 @@ func GetKMS() (k kms.KMS, err error) {
}

k, err = local.NewKMS(key)
case "transit":
case transit.Name:
k, err = transit.NewVaultTransit(viper.GetString("kms_transit_engine"), viper.GetString("kms_transit_key"))
default:
return nil, fmt.Errorf("failed to initialize KMS backend %s: %v", backend, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/server/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
)

func GetLocker() (l lock.Locker, err error) {
viper.SetDefault("lock_backend", "local")
viper.SetDefault("lock_backend", local.Name)
backend := viper.GetString("lock_backend")

switch backend {
case "local":
case local.Name:
l = local.NewLock()
case "redis":
case redis.Name:
l = redis.NewLock()
case "postgres":
case postgres.Name:
l, err = postgres.NewLock()
default:
err = fmt.Errorf("backend is not implemented")
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
)

func GetStorage() (s storage.Storage, err error) {
viper.SetDefault("storage_backend", "fs")
viper.SetDefault("storage_backend", filesystem.Name)
backend := viper.GetString("storage_backend")

switch backend {
case "fs":
case filesystem.Name:
viper.SetDefault("storage_fs_dir", "./states")
s, err = filesystem.NewFileSystemStorage(viper.GetString("storage_fs_dir"))
case "s3":
case s3.Name:
viper.SetDefault("storage_s3_endpoint", "s3.amazonaws.com")
viper.SetDefault("storage_s3_use_ssl", true)
viper.SetDefault("storage_s3_bucket", "terraform-state")
Expand Down
4 changes: 3 additions & 1 deletion pkg/storage/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/nimbolus/terraform-backend/pkg/terraform"
)

const Name = "fs"

type FileSystemStorage struct {
directory string
}
Expand All @@ -24,7 +26,7 @@ func NewFileSystemStorage(directory string) (*FileSystemStorage, error) {
}

func (f *FileSystemStorage) GetName() string {
return "file"
return Name
}

func (f *FileSystemStorage) SaveState(s *terraform.State) error {
Expand Down
4 changes: 3 additions & 1 deletion pkg/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/nimbolus/terraform-backend/pkg/terraform"
)

const Name = "s3"

type S3Storage struct {
client *minio.Client
bucket string
Expand Down Expand Up @@ -38,7 +40,7 @@ func NewS3Storage(endpoint, bucket, accessKey, secretKey string, useSSL bool) (*
}

func (s *S3Storage) GetName() string {
return "s3"
return Name
}

func (s *S3Storage) SaveState(state *terraform.State) error {
Expand Down

0 comments on commit dbd46fd

Please sign in to comment.