forked from nimbolus/terraform-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
109 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,55 @@ | ||
package jwt | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/coreos/go-oidc/v3/oidc" | ||
"github.com/nimbolus/terraform-backend/terraform" | ||
) | ||
|
||
type JWTAuth struct { | ||
issuerURL string | ||
} | ||
|
||
func NewJWTAuth(issuerURL string) *JWTAuth { | ||
return &JWTAuth{ | ||
issuerURL: issuerURL, | ||
} | ||
} | ||
|
||
func (l *JWTAuth) GetName() string { | ||
return "jwt" | ||
} | ||
|
||
func (b *JWTAuth) Authenticate(secret string, s *terraform.State) (bool, error) { | ||
provider, err := oidc.NewProvider(context.Background(), b.issuerURL) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
verifier := provider.Verifier(&oidc.Config{ | ||
SkipClientIDCheck: true, | ||
}) | ||
|
||
token, err := verifier.Verify(context.Background(), secret) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
var claims struct { | ||
TerraformBackend struct { | ||
Project string `json:"project"` | ||
State string `json:"state"` | ||
} `json:"terraform-backend"` | ||
} | ||
if err := token.Claims(&claims); err != nil { | ||
return false, err | ||
} | ||
|
||
tokenID := terraform.GetStateID(claims.TerraformBackend.Project, claims.TerraformBackend.State) | ||
if s.ID == tokenID { | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
package terraform | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
) | ||
|
||
type State struct { | ||
ID string | ||
Data []byte | ||
Lock []byte | ||
} | ||
|
||
func GetStateID(project, id string) string { | ||
path := fmt.Sprintf("%s-%s", project, id) | ||
hash := sha256.Sum256([]byte(path)) | ||
return fmt.Sprintf("%x", hash[:]) | ||
} |