Skip to content

Commit 9b6aa56

Browse files
authored
providers/radius: fix panic when no cert is configured (#17762)
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
1 parent 6b43ddc commit 9b6aa56

File tree

1 file changed

+72
-75
lines changed

1 file changed

+72
-75
lines changed

internal/outpost/radius/handler_eap.go

Lines changed: 72 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -41,95 +41,92 @@ func (pi *ProviderInstance) SetEAPState(key string, state *protocol.State) {
4141
}
4242

4343
func (pi *ProviderInstance) GetEAPSettings() protocol.Settings {
44-
protocols := []protocol.ProtocolConstructor{
45-
identity.Protocol,
46-
legacy_nak.Protocol,
44+
settings := protocol.Settings{
45+
Logger: &logrusAdapter{pi.log},
46+
Protocols: []protocol.ProtocolConstructor{
47+
identity.Protocol,
48+
legacy_nak.Protocol,
49+
},
4750
}
4851

4952
certId := pi.certId
5053
if certId == "" {
51-
return protocol.Settings{
52-
Protocols: protocols,
53-
}
54+
return settings
5455
}
5556

5657
cert := pi.s.cryptoStore.Get(certId)
5758
if cert == nil {
58-
return protocol.Settings{
59-
Protocols: protocols,
60-
}
59+
return settings
6160
}
6261

63-
return protocol.Settings{
64-
Logger: &logrusAdapter{entry: pi.log},
65-
Protocols: append(protocols, tls.Protocol, peap.Protocol),
66-
ProtocolPriority: []protocol.Type{
67-
identity.TypeIdentity,
68-
tls.TypeTLS,
69-
},
70-
ProtocolSettings: map[protocol.Type]interface{}{
71-
tls.TypeTLS: tls.Settings{
72-
Config: &ttls.Config{
73-
Certificates: []ttls.Certificate{*cert},
74-
ClientAuth: ttls.RequireAnyClientCert,
75-
},
76-
HandshakeSuccessful: func(ctx protocol.Context, certs []*x509.Certificate) protocol.Status {
77-
ident := ctx.GetProtocolState(identity.TypeIdentity).(*identity.State).Identity
62+
settings.Protocols = append(settings.Protocols, tls.Protocol, peap.Protocol)
63+
settings.ProtocolPriority = []protocol.Type{
64+
identity.TypeIdentity,
65+
tls.TypeTLS,
66+
}
67+
settings.ProtocolSettings = map[protocol.Type]any{
68+
tls.TypeTLS: tls.Settings{
69+
Config: &ttls.Config{
70+
Certificates: []ttls.Certificate{*cert},
71+
ClientAuth: ttls.RequireAnyClientCert,
72+
},
73+
HandshakeSuccessful: func(ctx protocol.Context, certs []*x509.Certificate) protocol.Status {
74+
ident := ctx.GetProtocolState(identity.TypeIdentity).(*identity.State).Identity
7875

79-
ctx.Log().Debug("Starting authn flow")
80-
pem := pem.EncodeToMemory(&pem.Block{
81-
Type: "CERTIFICATE",
82-
Bytes: certs[0].Raw,
83-
})
76+
ctx.Log().Debug("Starting authn flow")
77+
pem := pem.EncodeToMemory(&pem.Block{
78+
Type: "CERTIFICATE",
79+
Bytes: certs[0].Raw,
80+
})
8481

85-
fe := flow.NewFlowExecutor(context.Background(), pi.flowSlug, pi.s.ac.Client.GetConfig(), log.Fields{
86-
"client": utils.GetIP(ctx.Packet().RemoteAddr),
87-
"identity": ident,
88-
})
89-
fe.Answers[flow.StageIdentification] = ident
90-
fe.DelegateClientIP(utils.GetIP(ctx.Packet().RemoteAddr))
91-
fe.Params.Add("goauthentik.io/outpost/radius", "true")
92-
fe.AddHeader("X-Authentik-Outpost-Certificate", url.QueryEscape(string(pem)))
82+
fe := flow.NewFlowExecutor(context.Background(), pi.flowSlug, pi.s.ac.Client.GetConfig(), log.Fields{
83+
"client": utils.GetIP(ctx.Packet().RemoteAddr),
84+
"identity": ident,
85+
})
86+
fe.Answers[flow.StageIdentification] = ident
87+
fe.DelegateClientIP(utils.GetIP(ctx.Packet().RemoteAddr))
88+
fe.Params.Add("goauthentik.io/outpost/radius", "true")
89+
fe.AddHeader("X-Authentik-Outpost-Certificate", url.QueryEscape(string(pem)))
9390

94-
passed, err := fe.Execute()
95-
if err != nil {
96-
ctx.Log().Warn("failed to execute flow", "error", err)
97-
return protocol.StatusError
98-
}
99-
ctx.Log().Debug("Finished flow")
100-
if !passed {
101-
return protocol.StatusError
102-
}
103-
access, _, err := fe.ApiClient().OutpostsApi.OutpostsRadiusAccessCheck(context.Background(), pi.providerId).AppSlug(pi.appSlug).Execute()
104-
if err != nil {
105-
ctx.Log().Warn("failed to check access: %v", err)
106-
return protocol.StatusError
107-
}
108-
if !access.Access.Passing {
109-
ctx.Log().Info("Access denied for user")
110-
return protocol.StatusError
111-
}
112-
if access.HasAttributes() {
113-
ctx.AddResponseModifier(func(r, q *radius.Packet) error {
114-
rawData, err := base64.StdEncoding.DecodeString(access.GetAttributes())
115-
if err != nil {
116-
ctx.Log().Warn("failed to decode attributes from core: %v", err)
117-
return errors.New("attribute_decode_failed")
118-
}
119-
p, err := radius.Parse(rawData, pi.SharedSecret)
120-
if err != nil {
121-
ctx.Log().Warn("failed to parse attributes from core: %v", err)
122-
return errors.New("attribute_parse_failed")
123-
}
124-
for _, attr := range p.Attributes {
125-
r.Add(attr.Type, attr.Attribute)
126-
}
127-
return nil
128-
})
129-
}
130-
return protocol.StatusSuccess
131-
},
91+
passed, err := fe.Execute()
92+
if err != nil {
93+
ctx.Log().Warn("failed to execute flow", "error", err)
94+
return protocol.StatusError
95+
}
96+
ctx.Log().Debug("Finished flow")
97+
if !passed {
98+
return protocol.StatusError
99+
}
100+
access, _, err := fe.ApiClient().OutpostsApi.OutpostsRadiusAccessCheck(context.Background(), pi.providerId).AppSlug(pi.appSlug).Execute()
101+
if err != nil {
102+
ctx.Log().Warn("failed to check access: %v", err)
103+
return protocol.StatusError
104+
}
105+
if !access.Access.Passing {
106+
ctx.Log().Info("Access denied for user")
107+
return protocol.StatusError
108+
}
109+
if access.HasAttributes() {
110+
ctx.AddResponseModifier(func(r, q *radius.Packet) error {
111+
rawData, err := base64.StdEncoding.DecodeString(access.GetAttributes())
112+
if err != nil {
113+
ctx.Log().Warn("failed to decode attributes from core: %v", err)
114+
return errors.New("attribute_decode_failed")
115+
}
116+
p, err := radius.Parse(rawData, pi.SharedSecret)
117+
if err != nil {
118+
ctx.Log().Warn("failed to parse attributes from core: %v", err)
119+
return errors.New("attribute_parse_failed")
120+
}
121+
for _, attr := range p.Attributes {
122+
r.Add(attr.Type, attr.Attribute)
123+
}
124+
return nil
125+
})
126+
}
127+
return protocol.StatusSuccess
132128
},
133129
},
134130
}
131+
return settings
135132
}

0 commit comments

Comments
 (0)