|
| 1 | +package httpmw |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/subtle" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "golang.org/x/xerrors" |
| 9 | + |
| 10 | + "github.com/coder/coder/v2/coderd/database" |
| 11 | + "github.com/coder/coder/v2/coderd/database/dbauthz" |
| 12 | + "github.com/coder/coder/v2/coderd/httpapi" |
| 13 | + "github.com/coder/coder/v2/codersdk" |
| 14 | +) |
| 15 | + |
| 16 | +type provisionerDaemonContextKey struct{} |
| 17 | + |
| 18 | +func ProvisionerDaemonAuthenticated(r *http.Request) bool { |
| 19 | + proxy, ok := r.Context().Value(provisionerDaemonContextKey{}).(bool) |
| 20 | + return ok && proxy |
| 21 | +} |
| 22 | + |
| 23 | +type ExtractProvisionerAuthConfig struct { |
| 24 | + DB database.Store |
| 25 | + Optional bool |
| 26 | +} |
| 27 | + |
| 28 | +func ExtractProvisionerDaemonAuthenticated(opts ExtractProvisionerAuthConfig, psk string) func(next http.Handler) http.Handler { |
| 29 | + return func(next http.Handler) http.Handler { |
| 30 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 31 | + ctx := r.Context() |
| 32 | + |
| 33 | + handleOptional := func(code int, response codersdk.Response) { |
| 34 | + if opts.Optional { |
| 35 | + next.ServeHTTP(w, r) |
| 36 | + return |
| 37 | + } |
| 38 | + httpapi.Write(ctx, w, code, response) |
| 39 | + } |
| 40 | + |
| 41 | + if psk == "" { |
| 42 | + // No psk means external provisioner daemons are not allowed. |
| 43 | + // So their auth is not valid. |
| 44 | + handleOptional(http.StatusBadRequest, codersdk.Response{ |
| 45 | + Message: "External provisioner daemons not enabled", |
| 46 | + }) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + token := r.Header.Get(codersdk.ProvisionerDaemonPSK) |
| 51 | + if token == "" { |
| 52 | + handleOptional(http.StatusUnauthorized, codersdk.Response{ |
| 53 | + Message: "provisioner daemon auth token required", |
| 54 | + }) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + if subtle.ConstantTimeCompare([]byte(token), []byte(psk)) != 1 { |
| 59 | + handleOptional(http.StatusUnauthorized, codersdk.Response{ |
| 60 | + Message: "provisioner daemon auth token invalid", |
| 61 | + }) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // The PSK does not indicate a specific provisioner daemon. So just |
| 66 | + // store a boolean so the caller can check if the request is from an |
| 67 | + // authenticated provisioner daemon. |
| 68 | + ctx = context.WithValue(ctx, provisionerDaemonContextKey{}, true) |
| 69 | + // nolint:gocritic // Authenticating as a provisioner daemon. |
| 70 | + ctx = dbauthz.AsProvisionerd(ctx) |
| 71 | + subj, ok := dbauthz.ActorFromContext(ctx) |
| 72 | + if !ok { |
| 73 | + // This should never happen |
| 74 | + httpapi.InternalServerError(w, xerrors.New("developer error: ExtractProvisionerDaemonAuth missing rbac actor")) |
| 75 | + } |
| 76 | + |
| 77 | + // Use the same subject for the userAuthKey |
| 78 | + ctx = context.WithValue(ctx, userAuthKey{}, Authorization{ |
| 79 | + Actor: subj, |
| 80 | + ActorName: "provisioner_daemon", |
| 81 | + }) |
| 82 | + |
| 83 | + next.ServeHTTP(w, r.WithContext(ctx)) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
0 commit comments