-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforward_auth_grpc.go
149 lines (127 loc) · 3.86 KB
/
forward_auth_grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"os"
"github.com/http-wasm/http-wasm-guest-tinygo/handler"
"github.com/http-wasm/http-wasm-guest-tinygo/handler/api"
pb "github.com/morzan1001/forward_auth_grpc_plugin/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
)
// Config holds the configuration for the GRPCForwardAuth plugin.
type Config struct {
Address string `json:"address,omitempty"`
TokenHeader string `json:"tokenHeader,omitempty"`
UseTLS bool `json:"useTLS,omitempty"`
CACertPath string `json:"caCertPath,omitempty"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// GRPCForwardAuth plugin.
type GRPCForwardAuth struct {
address string
tokenHeader string
client pb.AuthServiceClient
}
// New creates a new GRPCForwardAuth plugin.
func New(config *Config) (*GRPCForwardAuth, error) {
if config.Address == "" {
return nil, status.Error(codes.InvalidArgument, "auth service address cannot be empty")
}
// Setup connection options
var opts []grpc.DialOption
if config.UseTLS {
var caCertPool *x509.CertPool
if config.CACertPath != "" {
// Load the CA certificates
caCert, err := os.ReadFile(config.CACertPath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to read CA certificate: %v", err)
}
// Create a CertPool and add the CA certificates
caCertPool = x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(caCert) {
return nil, status.Errorf(codes.Internal, "failed to append CA certificate")
}
} else {
// Use the system CA certificates
var err error
caCertPool, err = x509.SystemCertPool()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to load system CA certificates: %v", err)
}
}
// Create the TLS credentials
tlsConfig := &tls.Config{
RootCAs: caCertPool,
}
creds := credentials.NewTLS(tlsConfig)
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
conn, err := grpc.NewClient(config.Address, opts...)
if err != nil {
return nil, status.Errorf(codes.Unavailable, "failed to connect to auth service: %v", err)
}
client := pb.NewAuthServiceClient(conn)
return &GRPCForwardAuth{
address: config.Address,
tokenHeader: config.TokenHeader,
client: client,
}, nil
}
func (g *GRPCForwardAuth) handleRequest(req api.Request, resp api.Response) (next bool, reqCtx uint32) {
// Get token from header
token, ok := req.Headers().Get(g.tokenHeader)
if !ok || token == "" {
resp.SetStatusCode(401)
resp.Body().Write([]byte("missing token"))
return false, 0
}
// Create auth request
authReq := &pb.AuthRequest{
Token: token,
}
// Call auth service
ctx := context.Background()
authResp, err := g.client.Authenticate(ctx, authReq)
if err != nil || authResp == nil || !authResp.Allowed {
resp.SetStatusCode(401)
if authResp != nil && authResp.Message != "" {
resp.Body().Write([]byte(authResp.Message))
} else {
resp.Body().Write([]byte("authentication failed"))
}
return false, 0
}
// Add headers from auth response to the original request
for key, value := range authResp.Metadata {
req.Headers().Set(key, value)
}
return true, 0
}
// main is the entry point for the Wasm module.
func main() {
var config Config
err := json.Unmarshal(handler.Host.GetConfig(), &config)
if err != nil {
handler.Host.Log(api.LogLevelError, fmt.Sprintf("Could not load config %v", err))
os.Exit(1)
}
mw, err := New(&config)
if err != nil {
handler.Host.Log(api.LogLevelError, fmt.Sprintf("Could not load config %v", err))
os.Exit(1)
}
handler.HandleRequestFn = mw.handleRequest
}