fix IdP interface

This commit is contained in:
kkb0318
2024-03-30 20:20:16 +09:00
parent a64cc1ed95
commit de70831688
3 changed files with 25 additions and 20 deletions
+3 -1
View File
@@ -1,5 +1,7 @@
package selfhosted
import "context"
type OIDCIdProvider interface {
Discovery() ([]byte, error)
JWK() ([]byte, error)
@@ -8,5 +10,5 @@ type OIDCIdProvider interface {
type OIDCIdPCreator interface {
CreateStorage() error
Upload(o OIDCIdProvider) error
Upload(ctx context.Context, o OIDCIdProvider) error
}
+10 -9
View File
@@ -7,9 +7,10 @@ import (
"github.com/kkb0318/irsa-manager/internal/selfhosted"
)
type MyIdProvider struct {
type IdProvider struct {
jwk *selfhosted.JWK
issuerHostPath string
jwksFileName string
}
type OIDCDiscoveryConfiguration struct {
@@ -22,24 +23,24 @@ type OIDCDiscoveryConfiguration struct {
ClaimsSupported []string `json:"claims_supported"`
}
func (p *MyIdProvider) Discovery() ([]byte, error) {
func (p *IdProvider) Discovery() ([]byte, error) {
oidcConfig := OIDCDiscoveryConfiguration{
Issuer: fmt.Sprintf("https://%s/", p.issuerHostPath),
JWKSURI: fmt.Sprintf("https://%s/keys.json", p.issuerHostPath),
JWKSURI: fmt.Sprintf("https://%s/%s", p.issuerHostPath, p.jwksFileName),
AuthorizationEndpoint: "urn:kubernetes:programmatic_authorization",
ResponseTypesSupported: []string{"id_token"},
SubjectTypesSupported: []string{"public"},
IDTokenSigningAlgValuesSupported: []string{"RS256"},
ClaimsSupported: []string{"sub", "iss"},
}
jsonData, err := json.MarshalIndent(oidcConfig, "", " ")
jsonData, err := json.MarshalIndent(oidcConfig, "", " ")
if err != nil {
return nil, err
}
return jsonData, nil
}
func (p *MyIdProvider) JWK() ([]byte, error) {
func (p *IdProvider) JWK() ([]byte, error) {
jsonData, err := json.MarshalIndent(p.jwk.GetKeys(), "", " ")
if err != nil {
return nil, err
@@ -47,10 +48,10 @@ func (p *MyIdProvider) JWK() ([]byte, error) {
return jsonData, nil
}
func (p *MyIdProvider) Endpoint() []byte {
return []byte{}
func (p *IdProvider) Endpoint() string {
return ""
}
func NewMyIdProvider(jwk *selfhosted.JWK, issuerHostPath string) *MyIdProvider {
return &MyIdProvider{jwk, issuerHostPath}
func NewIdProvider(jwk *selfhosted.JWK, issuerHostPath, jwksFileName string) *IdProvider {
return &IdProvider{jwk, issuerHostPath, jwksFileName}
}
+12 -10
View File
@@ -16,17 +16,11 @@ type S3IdPCreator struct {
// NewS3IdPCreator initializes a new instance of S3IdPCreator with the specified AWS region and bucket name.
// This function attempts to create an AWS client configured for the specified region.
func NewS3IdPCreator(region, bucketName string) (*S3IdPCreator, error) {
ctx := context.Background()
awsConfig, err := client.NewAwsClient(ctx, region)
if err != nil {
return nil, fmt.Errorf("unable to load SDK config, %w", err)
}
func NewS3IdPCreator(awsConfig *client.AwsConfig, bucketName string) (*S3IdPCreator, error) {
s3Client := awsConfig.S3Cient(bucketName)
return &S3IdPCreator{s3Client}, nil
}
// CreateStorage creates an S3 bucket
func (s *S3IdPCreator) CreateStorage() error {
err := s.s3Client.CreateBucket(context.TODO())
@@ -39,18 +33,26 @@ func (s *S3IdPCreator) CreateStorage() error {
// Upload uploads the OIDC provider's discovery configuration and JSON Web Key Set (JWKS) to the specified AWS S3 bucket.
// This method is responsible for uploading the necessary OIDC configuration files to S3, making them accessible for OIDC clients.
func (s *S3IdPCreator) Upload(ctx context.Context, o selfhosted.OIDCIdProvider) error {
err := s.s3Client.PutObject(ctx,
discovery, err := o.Discovery()
if err != nil {
return nil
}
err = s.s3Client.PutObject(ctx,
CONFIGURATION_PATH,
o.Discovery(),
discovery,
)
if err != nil {
return fmt.Errorf("unable to upload discovery document, %w", err)
}
// Uplaod JWK
jwk, err := o.JWK()
if err != nil {
return nil
}
err = s.s3Client.PutObject(ctx,
"keys.json",
o.JWK(),
jwk,
)
if err != nil {
return fmt.Errorf("unable to upload JWK, %w", err)