mirror of
https://github.com/hanzo-ml/trainer.git
synced 2026-09-24 00:31:34 +00:00
* feat(api): add progress types to api Signed-off-by: Rob Bell <[email protected]> * feat: progress server stub Signed-off-by: Rob Bell <[email protected]> * feat: setup tls for server Signed-off-by: Rob Bell <[email protected]> * refactor: pull out middleware Signed-off-by: Rob Bell <[email protected]> * refactor: remove logger message noise from tests Signed-off-by: Rob Bell <[email protected]> * fix: send server error logs to main logger Signed-off-by: Rob Bell <[email protected]> * feat: update the trainer status Signed-off-by: Rob Bell <[email protected]> * feat: use separate client to avoid impacting main reconciler client-side rate limits Signed-off-by: Rob Bell <[email protected]> * feat: configure progress server in helm chart and configmap Signed-off-by: Rob Bell <[email protected]> * fix: prevent race condition Signed-off-by: Rob Bell <[email protected]> * feat: inject progress runtime config Signed-off-by: Rob Bell <[email protected]> * fix: progress client config Signed-off-by: Rob Bell <[email protected]> * fix: update generated config Signed-off-by: Rob Bell <[email protected]> * feat: validate auth tokens in endpoint Signed-off-by: Rob Bell <[email protected]> * test: validate auth tokens in endpoint Signed-off-by: Rob Bell <[email protected]> * refactor: tidy tests to use same format as other tests Signed-off-by: Rob Bell <[email protected]> * fix: look up the cluster issuer from the service account token Different clusters can use different issuers so look up the issuer from the trusted in-cluster service account token. Signed-off-by: Rob Bell <[email protected]> * refactor: rename authn middleware Signed-off-by: Rob Bell <[email protected]> * feat: add authn to status update endpoint Signed-off-by: Rob Bell <[email protected]> * refactor: tidy up reading in-cluster service account token Signed-off-by: Rob Bell <[email protected]> * fix: rbac permissions Signed-off-by: Rob Bell <[email protected]> * fix: better error response Signed-off-by: Rob Bell <[email protected]> * refactor: move kubebuilder annotation higher up Signed-off-by: Rob Bell <[email protected]> * fix: make lastUpdateTime required Signed-off-by: Rob Bell <[email protected]> * chore: fix config tests after rebase Signed-off-by: Rob Bell <[email protected]> * test: update config test with new progress server config Signed-off-by: Rob Bell <[email protected]> * test: add progress tracking e2e test Signed-off-by: Rob Bell <[email protected]> * fix: allow feature gates to be configurable from command line argument So they can be overridden in e2e tests Signed-off-by: Rob Bell <[email protected]> * refactor: reuse shared progress StatusUrl Signed-off-by: Rob Bell <[email protected]> * test: tidy up tests Signed-off-by: Rob Bell <[email protected]> * refactor: allow plugins to use config Signed-off-by: Rob Bell <[email protected]> * refactor: centralise shared constants Signed-off-by: Rob Bell <[email protected]> * fix: disable http2 in progress server tls opts if disabled by config Signed-off-by: Rob Bell <[email protected]> * fix: minor improvements suggested by copilot Signed-off-by: Rob Bell <[email protected]> * chore: rename progress server => status server Signed-off-by: Rob Bell <[email protected]> * chore: update type doc Signed-off-by: Rob Bell <[email protected]> * chore: update name of feature gate Signed-off-by: Rob Bell <[email protected]> * fix: after rebase Signed-off-by: Rob Bell <[email protected]> * chore: fix typo Signed-off-by: Rob Bell <[email protected]> * chore: rename handler Signed-off-by: Rob Bell <[email protected]> * fix: run status server on all replicas Signed-off-by: Rob Bell <[email protected]> * fix: deep copy the rest config Signed-off-by: Rob Bell <[email protected]> * refactor: use ssa Signed-off-by: Rob Bell <[email protected]> * refactor: separate token audience per train job Signed-off-by: Rob Bell <[email protected]> * chore: rename status server Signed-off-by: Rob Bell <[email protected]> * fix: revert adding status pkg to manifest gen paths Signed-off-by: Rob Bell <[email protected]> * chore: update log messages Signed-off-by: Rob Bell <[email protected]> * chore: update api docs Signed-off-by: Rob Bell <[email protected]> * chore: update format Signed-off-by: Rob Bell <[email protected]> * fix: revert inadvertent change Signed-off-by: Rob Bell <[email protected]> * chore: update api name and type docs Signed-off-by: Rob Bell <[email protected]> * refactor: initialise oidc provider in manager startup Signed-off-by: Rob Bell <[email protected]> * fix: allow empty update request Signed-off-by: Rob Bell <[email protected]> * fix: wait for server shut down Signed-off-by: Rob Bell <[email protected]> * fix: handle missing train job Signed-off-by: Rob Bell <[email protected]> * refactor: update env var names Signed-off-by: Rob Bell <[email protected]> * chore: rename packages, plugins, feature gate Signed-off-by: Rob Bell <[email protected]> * refactor: update env var names Signed-off-by: Rob Bell <[email protected]> * chore: regenerate after rebase Signed-off-by: Rob Bell <[email protected]> * chore: fixes after rebase Signed-off-by: Rob Bell <[email protected]> * chore: minor doc updates Signed-off-by: Rob Bell <[email protected]> * chore: rename status server config Signed-off-by: Rob Bell <[email protected]> * chore: replace placeholder config with nil Signed-off-by: Rob Bell <[email protected]> * chore: revert separate runtime utils package Signed-off-by: Rob Bell <[email protected]> * chore: remove unused config from tests Signed-off-by: Rob Bell <[email protected]> --------- Signed-off-by: Rob Bell <[email protected]>
155 lines
4.3 KiB
Go
155 lines
4.3 KiB
Go
/*
|
|
Copyright 2026 The Kubeflow Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package statusserver
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/coreos/go-oidc/v3/oidc"
|
|
"k8s.io/client-go/rest"
|
|
)
|
|
|
|
type TokenAuthorizer interface {
|
|
Init(ctx context.Context) error
|
|
Authorize(ctx context.Context, rawIDToken, namespace, trainJobName string) (bool, error)
|
|
}
|
|
|
|
type projectedServiceAccountTokenAuthorizer struct {
|
|
oidcProvider *oidc.Provider
|
|
config *rest.Config
|
|
}
|
|
|
|
var _ TokenAuthorizer = &projectedServiceAccountTokenAuthorizer{}
|
|
|
|
// projectedToken is the decoded JWT claims of a k8s projected service account token.
|
|
// Note: we only decode the subset of claims we actually need.
|
|
type projectedToken struct {
|
|
Issuer string `json:"iss"`
|
|
Kubernetes struct {
|
|
Namespace string `json:"namespace"`
|
|
} `json:"kubernetes.io"`
|
|
}
|
|
|
|
// NewProjectedServiceAccountTokenAuthorizer creates a validator for checking a bearer token has permission to
|
|
// update the requested train job.
|
|
func NewProjectedServiceAccountTokenAuthorizer(config *rest.Config) TokenAuthorizer {
|
|
return &projectedServiceAccountTokenAuthorizer{
|
|
config: config,
|
|
}
|
|
}
|
|
|
|
func (p *projectedServiceAccountTokenAuthorizer) Init(ctx context.Context) error {
|
|
issuerURL, err := getClusterOIDCIssuerURL()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to discover issuer URL: %w", err)
|
|
}
|
|
|
|
// Create an authenticated HTTP client using the provided rest config
|
|
httpClient, err := rest.HTTPClientFor(p.config)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create HTTP client: %w", err)
|
|
}
|
|
|
|
// Create context with the authenticated HTTP client
|
|
ctx = oidc.ClientContext(ctx, httpClient)
|
|
|
|
provider, err := oidc.NewProvider(ctx, issuerURL)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create OIDC provider: %w", err)
|
|
}
|
|
p.oidcProvider = provider
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *projectedServiceAccountTokenAuthorizer) Authorize(ctx context.Context, authHeader, namespace, trainJobName string) (bool, error) {
|
|
if p.oidcProvider == nil {
|
|
return false, fmt.Errorf("OIDC provider has not been initialized")
|
|
}
|
|
|
|
rawToken := extractRawToken(authHeader)
|
|
|
|
// Create authorizer with TrainJob-specific audience
|
|
expectedAudience := TokenAudience(namespace, trainJobName)
|
|
verifier := p.oidcProvider.Verifier(&oidc.Config{
|
|
ClientID: expectedAudience,
|
|
})
|
|
|
|
// Check token signature, expiry, and audience
|
|
idToken, err := verifier.Verify(ctx, rawToken)
|
|
if err != nil {
|
|
return false, nil
|
|
}
|
|
|
|
// Check token is bound to a pod in the same namespace as the train job
|
|
parsedToken := projectedToken{}
|
|
err = idToken.Claims(&parsedToken)
|
|
if err != nil {
|
|
return false, nil
|
|
}
|
|
if parsedToken.Kubernetes.Namespace != namespace {
|
|
return false, nil
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func extractRawToken(authHeader string) string {
|
|
parts := strings.Split(authHeader, " ")
|
|
|
|
if len(parts) != 2 || parts[0] != "Bearer" {
|
|
return ""
|
|
}
|
|
|
|
return parts[1]
|
|
}
|
|
|
|
// getClusterOIDCIssuerURL tries to look up the cluster token issuer from the in-cluster service account token
|
|
// Different clusters may use different issuers. This is a reliable way of discovering the issuer.
|
|
func getClusterOIDCIssuerURL() (string, error) {
|
|
tokenBytes, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
parts := strings.Split(strings.TrimSpace(string(tokenBytes)), ".")
|
|
if len(parts) != 3 {
|
|
return "", fmt.Errorf("serviceaccount token is not a jwt")
|
|
}
|
|
|
|
payload, err := base64.RawURLEncoding.DecodeString(parts[1])
|
|
if err != nil {
|
|
return "", fmt.Errorf("serviceaccount token is not a jwt: %w", err)
|
|
}
|
|
|
|
var token projectedToken
|
|
if err := json.Unmarshal(payload, &token); err != nil {
|
|
return "", fmt.Errorf("serviceaccount token is not a jwt: %w", err)
|
|
}
|
|
|
|
if token.Issuer == "" {
|
|
return "", fmt.Errorf("serviceaccount token missing issuer claim")
|
|
}
|
|
|
|
return token.Issuer, nil
|
|
}
|