Skip to content

Commit

Permalink
add project-level access for jwt auth
Browse files Browse the repository at this point in the history
  • Loading branch information
lu1as committed Apr 3, 2022
1 parent 5120200 commit 56429cb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions cmd/terraform-backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func stateHandler(store storage.Storage, locker lock.Locker, kms kms.KMS) func(h

vars := mux.Vars(req)
state := &terraform.State{
ID: terraform.GetStateID(vars["project"], vars["id"]),
ID: terraform.GetStateID(vars["project"], vars["name"]),
Project: vars["project"],
Name: vars["name"],
}

log.Infof("%s %s", req.Method, req.URL.Path)
Expand Down Expand Up @@ -171,7 +173,7 @@ func main() {
tlsCert := viper.GetString("tls_cert")

r := mux.NewRouter().StrictSlash(true)
r.HandleFunc("/state/{project}/{id}", stateHandler(store, locker, kms))
r.HandleFunc("/state/{project}/{name}", stateHandler(store, locker, kms))
r.HandleFunc("/health", healthHandler)

if tlsKey != "" && tlsCert != "" {
Expand Down
2 changes: 2 additions & 0 deletions docs/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ JWT allow granting access to a state for a given time (the token lifetime). The
}
```

NOTE: `state` value can be set to `*` to allow accessing all project states

### Config
| Environment Variable | Type | Example | Description |
|--------------------------|------|----------------------------------------------|-----------------------------------------------------------------------------------|
Expand Down
5 changes: 3 additions & 2 deletions terraform/auth/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func (b *JWTAuth) Authenticate(secret string, s *terraform.State) (bool, error)
return false, err
}

tokenID := terraform.GetStateID(claims.TerraformBackend.Project, claims.TerraformBackend.State)
if s.ID == tokenID {
if s.Project == claims.TerraformBackend.Project && claims.TerraformBackend.State == "*" {
return true, nil
} else if s.Project == claims.TerraformBackend.Project && s.Name == claims.TerraformBackend.State {
return true, nil
}

Expand Down
8 changes: 5 additions & 3 deletions terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
)

type State struct {
ID string
Data []byte
Lock []byte
ID string
Data []byte
Lock []byte
Project string
Name string
}

func GetStateID(project, id string) string {
Expand Down

0 comments on commit 56429cb

Please sign in to comment.