|
1 | 1 | package beholder |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "crypto" |
5 | 6 | "crypto/ed25519" |
6 | 7 | "crypto/rand" |
| 8 | + "encoding/binary" |
7 | 9 | "fmt" |
| 10 | + "maps" |
| 11 | + "sync" |
| 12 | + "sync/atomic" |
| 13 | + "time" |
8 | 14 |
|
9 | 15 | "github.com/smartcontractkit/chainlink-common/pkg/chipingress" |
| 16 | + "google.golang.org/grpc" |
| 17 | + "google.golang.org/grpc/credentials" |
10 | 18 | ) |
11 | 19 |
|
12 | 20 | // authHeaderKey is the name of the header that the node authenticator will use to send the auth token |
13 | 21 | var authHeaderKey = "X-Beholder-Node-Auth-Token" |
14 | 22 |
|
15 | 23 | // authHeaderVersion is the version of the auth header format |
16 | 24 | var authHeaderVersion = "1" |
| 25 | +var authHeaderV2 = "2" |
17 | 26 |
|
18 | | -type staticAuthHeaderProvider struct { |
19 | | - headers map[string]string |
| 27 | +type HeaderProvider interface { |
| 28 | + Headers(ctx context.Context) (map[string]string, error) |
20 | 29 | } |
21 | 30 |
|
22 | | -func (p *staticAuthHeaderProvider) GetHeaders() map[string]string { |
23 | | - return p.headers |
| 31 | +type PerRPCCredentialsProvider interface { |
| 32 | + Credentials() credentials.PerRPCCredentials |
24 | 33 | } |
25 | 34 |
|
| 35 | +type Auth interface { |
| 36 | + PerRPCCredentialsProvider |
| 37 | + HeaderProvider |
| 38 | +} |
| 39 | + |
| 40 | +type Signer interface { |
| 41 | + Sign(ctx context.Context, keyID []byte, data []byte) ([]byte, error) |
| 42 | +} |
| 43 | + |
| 44 | +type staticAuth struct { |
| 45 | + headers map[string]string |
| 46 | + requireTransportSecurity bool |
| 47 | +} |
| 48 | + |
| 49 | +func (p *staticAuth) Headers(_ context.Context) (map[string]string, error) { |
| 50 | + return p.headers, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (p *staticAuth) Credentials() credentials.PerRPCCredentials { |
| 54 | + return p |
| 55 | +} |
| 56 | + |
| 57 | +func (p *staticAuth) GetRequestMetadata(ctx context.Context, _ ...string) (map[string]string, error) { |
| 58 | + return p.Headers(ctx) |
| 59 | +} |
| 60 | + |
| 61 | +func (p *staticAuth) RequireTransportSecurity() bool { |
| 62 | + return p.requireTransportSecurity |
| 63 | +} |
| 64 | + |
| 65 | +func NewStaticAuth(headers map[string]string, requireTransportSecurity bool) Auth { |
| 66 | + return &staticAuth{headers, requireTransportSecurity} |
| 67 | +} |
| 68 | + |
| 69 | +// Deprecated: use NewStaticAuth instead |
26 | 70 | func NewStaticAuthHeaderProvider(headers map[string]string) chipingress.HeaderProvider { |
27 | | - return &staticAuthHeaderProvider{headers: headers} |
| 71 | + return &staticAuth{headers: headers} |
| 72 | +} |
| 73 | + |
| 74 | +type rotatingAuth struct { |
| 75 | + csaPubKey ed25519.PublicKey |
| 76 | + signer Signer |
| 77 | + signerTimeout time.Duration |
| 78 | + headers atomic.Value // stores map[string]string |
| 79 | + ttl time.Duration |
| 80 | + lastUpdatedNanos atomic.Int64 |
| 81 | + requireTransportSecurity bool |
| 82 | + mu sync.Mutex |
| 83 | +} |
| 84 | + |
| 85 | +func NewRotatingAuth(csaPubKey ed25519.PublicKey, signer Signer, ttl time.Duration, requireTransportSecurity bool) Auth { |
| 86 | + r := &rotatingAuth{ |
| 87 | + csaPubKey: csaPubKey, |
| 88 | + signer: signer, |
| 89 | + signerTimeout: time.Second * 5, |
| 90 | + ttl: ttl, |
| 91 | + lastUpdatedNanos: atomic.Int64{}, |
| 92 | + requireTransportSecurity: requireTransportSecurity, |
| 93 | + } |
| 94 | + r.headers.Store(make(map[string]string)) |
| 95 | + return r |
| 96 | +} |
| 97 | + |
| 98 | +func (r *rotatingAuth) Headers(ctx context.Context) (map[string]string, error) { |
| 99 | + |
| 100 | + // Return a copy of the headers to avoid concurrent read/write to the map by callers |
| 101 | + returnHeader := make(map[string]string) |
| 102 | + lastUpdated := time.Unix(0, r.lastUpdatedNanos.Load()) |
| 103 | + |
| 104 | + if time.Since(lastUpdated) > r.ttl { |
| 105 | + |
| 106 | + r.mu.Lock() |
| 107 | + defer r.mu.Unlock() |
| 108 | + |
| 109 | + // Multiple concurrent calls (after the first) will block waiting for the lock. |
| 110 | + // First will get the lock and update headers + lastUpdated, double check since potentially another goroutine has already |
| 111 | + // updated the headers and lastUpdated while waiting for the lock. |
| 112 | + lastUpdated = time.Unix(0, r.lastUpdatedNanos.Load()) |
| 113 | + if time.Since(lastUpdated) < r.ttl { |
| 114 | + maps.Copy(returnHeader, r.headers.Load().(map[string]string)) |
| 115 | + return returnHeader, nil |
| 116 | + } |
| 117 | + |
| 118 | + // Append the bytes of the public key with bytes of the timestamp to create the message to sign |
| 119 | + ts := time.Now() |
| 120 | + tsBytes := make([]byte, 8) |
| 121 | + binary.BigEndian.PutUint64(tsBytes, uint64(ts.UnixNano())) |
| 122 | + msgBytes := append(r.csaPubKey, tsBytes...) |
| 123 | + |
| 124 | + ctxWithTimeout, cancel := context.WithTimeout(ctx, r.signerTimeout) |
| 125 | + defer cancel() |
| 126 | + |
| 127 | + // Sign(public key bytes + timestamp bytes) |
| 128 | + signature, err := r.signer.Sign(ctxWithTimeout, r.csaPubKey, msgBytes) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("beholder: failed to sign auth header: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + newHeaders := make(map[string]string) |
| 134 | + newHeaders[authHeaderKey] = fmt.Sprintf("%s:%x:%d:%x", authHeaderV2, r.csaPubKey, ts.UnixNano(), signature) |
| 135 | + |
| 136 | + r.headers.Store(newHeaders) |
| 137 | + r.lastUpdatedNanos.Store(ts.UnixNano()) |
| 138 | + } |
| 139 | + |
| 140 | + maps.Copy(returnHeader, r.headers.Load().(map[string]string)) |
| 141 | + |
| 142 | + return returnHeader, nil |
| 143 | +} |
| 144 | + |
| 145 | +func (a *rotatingAuth) Credentials() credentials.PerRPCCredentials { |
| 146 | + return a |
| 147 | +} |
| 148 | + |
| 149 | +func (a *rotatingAuth) GetRequestMetadata(ctx context.Context, _ ...string) (map[string]string, error) { |
| 150 | + return a.Headers(ctx) |
| 151 | +} |
| 152 | + |
| 153 | +func (a *rotatingAuth) RequireTransportSecurity() bool { |
| 154 | + return a.requireTransportSecurity |
28 | 155 | } |
29 | 156 |
|
30 | 157 | // BuildAuthHeaders creates the auth header value to be included on requests. |
@@ -58,3 +185,7 @@ func NewAuthHeaders(ed25519Signer crypto.Signer) (map[string]string, error) { |
58 | 185 |
|
59 | 186 | return map[string]string{authHeaderKey: headerValue}, nil |
60 | 187 | } |
| 188 | + |
| 189 | +func authDialOpt(auth PerRPCCredentialsProvider) grpc.DialOption { |
| 190 | + return grpc.WithPerRPCCredentials(auth.Credentials()) |
| 191 | +} |
0 commit comments