|
| 1 | +package keyfetch |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rsa" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/lestrrat-go/jwx/v3/jwk" |
| 13 | + |
| 14 | + "github.com/jetstack/preflight/internal/cyberark/servicediscovery" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + // minRSAKeySize is the minimum RSA key size in bits; we'd expect that keys will be larger but 2048 is a sane floor |
| 19 | + // to enforce to ensure that a weak key can't accidentally be used |
| 20 | + minRSAKeySize = 2048 |
| 21 | +) |
| 22 | + |
| 23 | +// KeyFetcher is an interface for fetching public keys. |
| 24 | +type KeyFetcher interface { |
| 25 | + // FetchKey retrieves a public key from the key source. |
| 26 | + FetchKey(ctx context.Context) (PublicKey, error) |
| 27 | +} |
| 28 | + |
| 29 | +// Compile-time check that Client implements KeyFetcher |
| 30 | +var _ KeyFetcher = (*Client)(nil) |
| 31 | + |
| 32 | +// PublicKey represents an RSA public key retrieved from the key server. |
| 33 | +type PublicKey struct { |
| 34 | + // KeyID is the unique identifier for this key |
| 35 | + KeyID string |
| 36 | + |
| 37 | + // Key is the actual RSA public key |
| 38 | + Key *rsa.PublicKey |
| 39 | +} |
| 40 | + |
| 41 | +// Client fetches public keys from a CyberArk HTTP endpoint that provides keys in JWKS format. |
| 42 | +// It can be expanded in future to support other key types and formats, but for now it only supports RSA keys |
| 43 | +// and ignored other types. |
| 44 | +type Client struct { |
| 45 | + discoveryClient *servicediscovery.Client |
| 46 | + |
| 47 | + // httpClient is the HTTP client used for requests |
| 48 | + httpClient *http.Client |
| 49 | +} |
| 50 | + |
| 51 | +// NewClient creates a new key fetching client. |
| 52 | +// Uses CyberArk service discovery to derive the JWKS endpoint |
| 53 | +func NewClient(discoveryClient *servicediscovery.Client) *Client { |
| 54 | + return &Client{ |
| 55 | + discoveryClient: discoveryClient, |
| 56 | + httpClient: &http.Client{ |
| 57 | + Timeout: 10 * time.Second, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// FetchKey retrieves the public keys from the configured endpoint. |
| 63 | +// It returns a slice of PublicKey structs containing the key material and metadata. |
| 64 | +func (c *Client) FetchKey(ctx context.Context) (PublicKey, error) { |
| 65 | + services, _, err := c.discoveryClient.DiscoverServices(ctx) |
| 66 | + if err != nil { |
| 67 | + return PublicKey{}, fmt.Errorf("failed to get services from discovery client: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + endpoint, err := url.JoinPath(services.DiscoveryContext.API, "foo") |
| 71 | + if err != nil { |
| 72 | + return PublicKey{}, fmt.Errorf("failed to construct endpoint URL: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) |
| 76 | + if err != nil { |
| 77 | + return PublicKey{}, fmt.Errorf("failed to create request: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + req.Header.Set("Accept", "application/json") |
| 81 | + |
| 82 | + resp, err := c.httpClient.Do(req) |
| 83 | + if err != nil { |
| 84 | + return PublicKey{}, fmt.Errorf("failed to fetch keys from %s: %w", endpoint, err) |
| 85 | + } |
| 86 | + defer resp.Body.Close() |
| 87 | + |
| 88 | + if resp.StatusCode != http.StatusOK { |
| 89 | + body, _ := io.ReadAll(resp.Body) |
| 90 | + return PublicKey{}, fmt.Errorf("unexpected status code %d from %s: %s", resp.StatusCode, endpoint, string(body)) |
| 91 | + } |
| 92 | + |
| 93 | + body, err := io.ReadAll(resp.Body) |
| 94 | + if err != nil { |
| 95 | + return PublicKey{}, fmt.Errorf("failed to read response body: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + keySet, err := jwk.Parse(body) |
| 99 | + if err != nil { |
| 100 | + return PublicKey{}, fmt.Errorf("failed to parse JWKs response: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + for i := range keySet.Len() { |
| 104 | + key, ok := keySet.Key(i) |
| 105 | + if !ok { |
| 106 | + continue |
| 107 | + } |
| 108 | + |
| 109 | + // Only process RSA keys |
| 110 | + if key.KeyType().String() != "RSA" { |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + var rawKey any |
| 115 | + if err := jwk.Export(key, &rawKey); err != nil { |
| 116 | + // skip unparseable keys |
| 117 | + continue |
| 118 | + } |
| 119 | + |
| 120 | + rsaKey, ok := rawKey.(*rsa.PublicKey) |
| 121 | + if !ok { |
| 122 | + // only process RSA keys (for now) |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + if rsaKey.N.BitLen() < minRSAKeySize { |
| 127 | + // skip keys that are too small to be secure |
| 128 | + continue |
| 129 | + } |
| 130 | + |
| 131 | + kid, ok := key.KeyID() |
| 132 | + if !ok { |
| 133 | + // skip any keys which don't have an ID |
| 134 | + continue |
| 135 | + } |
| 136 | + |
| 137 | + alg, ok := key.Algorithm() |
| 138 | + if !ok { |
| 139 | + // skip any keys which don't have an algorithm specified |
| 140 | + continue |
| 141 | + } |
| 142 | + |
| 143 | + if alg.String() != "RSA-OAEP-256" { |
| 144 | + // we only use RSA keys for RSA-OAEP-256 |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + // return the first valid key we find |
| 149 | + return PublicKey{ |
| 150 | + KeyID: kid, |
| 151 | + Key: rsaKey, |
| 152 | + }, nil |
| 153 | + } |
| 154 | + |
| 155 | + return PublicKey{}, fmt.Errorf("no valid RSA keys found at %s", endpoint) |
| 156 | +} |
0 commit comments