mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
Goes from 135 MB binary with 1153 files importing go.temporal.io and 224
importing google.golang.org/grpc to a 10 MB native binary with ZERO of
either.
Deleted:
- tasks/, tasktests/, service/, chasm/, client/, api/, proto/, tools/
- cmd/tools/, components/, docker/
- schema/{cassandra,elasticsearch,mysql,postgresql,sqlite}/
- 40 tainted common/ subdirs + 7 top-level common files
- tests/testutils (no longer referenced after surviving deps drop common)
New cmd/tasksd (~100 lines) boots tasks.Embed (zap.Node) + http.Server.
New pkg/tasks/embed.go registers ZAP handlers for the SDK opcode surface
(0x0060-0x00A5). Workflow handlers return a 501 "not yet implemented"
envelope; native engine lands behind this contract.
Build: go build ./cmd/tasksd → 10 MB
Vet: go vet ./... → clean
Boot: tasksd --zap-port 9999 --http :7243
Probes: /healthz, /v1/tasks/health → 200
UI: /_/tasks/, / → 200 (embedded Vite bundle)
ZAP: _tasks._tcp on :9999 (mDNS disabled; direct connect)
go.mod: tidy. No go.temporal.io. No google.golang.org/grpc.
192 lines
4.5 KiB
Go
192 lines
4.5 KiB
Go
package authorization
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/rsa"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/go-jose/go-jose/v4"
|
|
"github.com/golang-jwt/jwt/v4"
|
|
"go.temporal.io/server/common/config"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
)
|
|
|
|
// Default token key provider
|
|
type defaultTokenKeyProvider struct {
|
|
config config.JWTKeyProvider
|
|
rsaKeys map[string]*rsa.PublicKey
|
|
ecKeys map[string]*ecdsa.PublicKey
|
|
keysLock sync.RWMutex
|
|
ticker *time.Ticker
|
|
logger log.Logger
|
|
stop chan bool
|
|
}
|
|
|
|
var _ TokenKeyProvider = (*defaultTokenKeyProvider)(nil)
|
|
|
|
func NewDefaultTokenKeyProvider(cfg *config.Authorization, logger log.Logger) *defaultTokenKeyProvider {
|
|
provider := defaultTokenKeyProvider{config: cfg.JWTKeyProvider, logger: logger}
|
|
provider.initialize()
|
|
return &provider
|
|
}
|
|
|
|
func (a *defaultTokenKeyProvider) initialize() {
|
|
a.rsaKeys = make(map[string]*rsa.PublicKey)
|
|
a.ecKeys = make(map[string]*ecdsa.PublicKey)
|
|
if a.config.HasSourceURIsConfigured() {
|
|
err := a.updateKeys()
|
|
if err != nil {
|
|
a.logger.Error("error during initial retrieval of token keys: ", tag.Error(err))
|
|
}
|
|
}
|
|
if a.config.RefreshInterval > 0 {
|
|
a.stop = make(chan bool)
|
|
a.ticker = time.NewTicker(a.config.RefreshInterval)
|
|
go a.timerCallback()
|
|
}
|
|
}
|
|
|
|
func (a *defaultTokenKeyProvider) Close() {
|
|
a.ticker.Stop()
|
|
a.stop <- true
|
|
close(a.stop)
|
|
}
|
|
|
|
func (a *defaultTokenKeyProvider) RsaKey(alg string, kid string) (*rsa.PublicKey, error) {
|
|
if !strings.EqualFold(alg, jwt.SigningMethodRS256.Name) {
|
|
return nil, fmt.Errorf("unexpected signing algorithm: %s", alg)
|
|
}
|
|
|
|
a.keysLock.RLock()
|
|
key, found := a.rsaKeys[kid]
|
|
a.keysLock.RUnlock()
|
|
if !found {
|
|
return nil, fmt.Errorf("RSA key not found for key ID: %s", kid)
|
|
}
|
|
return key, nil
|
|
}
|
|
|
|
func (a *defaultTokenKeyProvider) EcdsaKey(alg string, kid string) (*ecdsa.PublicKey, error) {
|
|
if !strings.EqualFold(alg, jwt.SigningMethodES256.Name) {
|
|
return nil, fmt.Errorf("unexpected signing algorithm: %s", alg)
|
|
}
|
|
|
|
a.keysLock.RLock()
|
|
key, found := a.ecKeys[kid]
|
|
a.keysLock.RUnlock()
|
|
if !found {
|
|
return nil, fmt.Errorf("ECDSA key not found for key ID: %s", kid)
|
|
}
|
|
return key, nil
|
|
}
|
|
|
|
func (a *defaultTokenKeyProvider) SupportedMethods() []string {
|
|
return []string{jwt.SigningMethodRS256.Name, jwt.SigningMethodES256.Name}
|
|
}
|
|
|
|
func (a *defaultTokenKeyProvider) timerCallback() {
|
|
for {
|
|
select {
|
|
case <-a.stop:
|
|
return
|
|
case <-a.ticker.C:
|
|
}
|
|
if a.config.HasSourceURIsConfigured() {
|
|
err := a.updateKeys()
|
|
if err != nil {
|
|
a.logger.Error("error while refreshing token keys: ", tag.Error(err))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (a *defaultTokenKeyProvider) updateKeys() error {
|
|
if !a.config.HasSourceURIsConfigured() {
|
|
return fmt.Errorf("no URIs configured for retrieving token keys")
|
|
}
|
|
|
|
rsaKeys := make(map[string]*rsa.PublicKey)
|
|
ecKeys := make(map[string]*ecdsa.PublicKey)
|
|
|
|
for _, uri := range a.config.KeySourceURIs {
|
|
if strings.TrimSpace(uri) == "" {
|
|
continue
|
|
}
|
|
err := a.updateKeysFromURI(uri, rsaKeys, ecKeys)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// swap old keys with the new ones
|
|
a.keysLock.Lock()
|
|
a.rsaKeys = rsaKeys
|
|
a.ecKeys = ecKeys
|
|
a.keysLock.Unlock()
|
|
return nil
|
|
}
|
|
|
|
func (a *defaultTokenKeyProvider) updateKeysFromURI(
|
|
uri string,
|
|
rsaKeys map[string]*rsa.PublicKey,
|
|
ecKeys map[string]*ecdsa.PublicKey,
|
|
) (err error) {
|
|
|
|
resp, err := a.openURI(uri)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
err = errors.Join(err, resp.Close())
|
|
}()
|
|
|
|
jwks := jose.JSONWebKeySet{}
|
|
if err := json.NewDecoder(resp).Decode(&jwks); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, k := range jwks.Keys {
|
|
switch k.Key.(type) {
|
|
case *rsa.PublicKey:
|
|
rsaKeys[k.KeyID] = k.Key.(*rsa.PublicKey)
|
|
case *ecdsa.PublicKey:
|
|
ecKeys[k.KeyID] = k.Key.(*ecdsa.PublicKey)
|
|
default:
|
|
a.logger.Warn(fmt.Sprintf("unexpected type of JWKS public key %s", k.Algorithm))
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// openURI returns a ReadCloser for the given URI. Supports http://, https://, and file:// schemes.
|
|
func (a *defaultTokenKeyProvider) openURI(uri string) (io.ReadCloser, error) {
|
|
u, err := url.Parse(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if u.Scheme == "file" {
|
|
if u.Host != "" && u.Host != "localhost" {
|
|
return nil, fmt.Errorf("file URI with remote host is not supported: %s", uri)
|
|
}
|
|
return os.Open(u.Path)
|
|
}
|
|
resp, err := http.Get(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return resp.Body, nil
|
|
}
|
|
|
|
func (a *defaultTokenKeyProvider) HmacKey(alg string, kid string) ([]byte, error) {
|
|
return nil, fmt.Errorf("unsupported key type HMAC for: %s", alg)
|
|
}
|