@@ -2,6 +2,7 @@ package sshportalapi
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"errors"
6
7
"log/slog"
7
8
"time"
@@ -23,20 +24,30 @@ var (
23
24
})
24
25
)
25
26
27
+ var (
28
+ falseResponse = []byte (`false` )
29
+ trueResponse = []byte (`true` )
30
+ )
31
+
26
32
func sshportal (
27
33
ctx context.Context ,
28
34
log * slog.Logger ,
29
- c * nats.EncodedConn ,
35
+ c * nats.Conn ,
30
36
p * rbac.Permission ,
31
37
l LagoonDBService ,
32
38
k KeycloakService ,
33
- ) nats.Handler {
34
- return func (_ , replySubject string , query * bus. SSHAccessQuery ) {
39
+ ) nats.MsgHandler {
40
+ return func (msg * nats. Msg ) {
35
41
var realmRoles , userGroups []string
36
42
// set up tracing and update metrics
37
43
ctx , span := otel .Tracer (pkgName ).Start (ctx , bus .SubjectSSHAccessQuery )
38
44
defer span .End ()
39
45
requestsCounter .Inc ()
46
+ var query bus.SSHAccessQuery
47
+ if err := json .Unmarshal (msg .Data , & query ); err != nil {
48
+ log .Warn ("couldn't unmarshal query" , slog .Any ("query" , msg .Data ))
49
+ return
50
+ }
40
51
log := log .With (slog .Any ("query" , query ))
41
52
// sanity check the query
42
53
if query .SSHFingerprint == "" || query .NamespaceName == "" {
@@ -48,7 +59,7 @@ func sshportal(
48
59
if err != nil {
49
60
if errors .Is (err , lagoondb .ErrNoResult ) {
50
61
log .Warn ("unknown namespace name" , slog .Any ("error" , err ))
51
- if err = c .Publish (replySubject , false ); err != nil {
62
+ if err = c .Publish (msg . Reply , falseResponse ); err != nil {
52
63
log .Error ("couldn't publish reply" , slog .Any ("error" , err ))
53
64
}
54
65
return
@@ -65,7 +76,7 @@ func sshportal(
65
76
log .Warn ("ID mismatch in environment identification" ,
66
77
slog .Any ("env" , env ),
67
78
slog .Any ("error" , err ))
68
- if err = c .Publish (replySubject , false ); err != nil {
79
+ if err = c .Publish (msg . Reply , falseResponse ); err != nil {
69
80
log .Error ("couldn't publish reply" , slog .Any ("error" , err ))
70
81
}
71
82
return
@@ -75,7 +86,7 @@ func sshportal(
75
86
if err != nil {
76
87
if errors .Is (err , lagoondb .ErrNoResult ) {
77
88
log .Debug ("unknown SSH Fingerprint" , slog .Any ("error" , err ))
78
- if err = c .Publish (replySubject , false ); err != nil {
89
+ if err = c .Publish (msg . Reply , falseResponse ); err != nil {
79
90
log .Error ("couldn't publish reply" , slog .Any ("error" , err ))
80
91
}
81
92
return
@@ -115,10 +126,13 @@ func sshportal(
115
126
ok := p .UserCanSSHToEnvironment (
116
127
ctx , env , realmRoles , userGroups , groupNameProjectIDsMap )
117
128
var logMsg string
129
+ var response []byte
118
130
if ok {
119
131
logMsg = "SSH access authorized"
132
+ response = trueResponse
120
133
} else {
121
134
logMsg = "SSH access not authorized"
135
+ response = falseResponse
122
136
}
123
137
log .Info (logMsg ,
124
138
slog .Int ("environmentID" , env .ID ),
@@ -127,7 +141,7 @@ func sshportal(
127
141
slog .String ("projectName" , env .ProjectName ),
128
142
slog .String ("userUUID" , user .UUID .String ()),
129
143
)
130
- if err = c .Publish (replySubject , ok ); err != nil {
144
+ if err = c .Publish (msg . Reply , response ); err != nil {
131
145
log .Error ("couldn't publish reply" ,
132
146
slog .String ("userUUID" , user .UUID .String ()),
133
147
slog .Any ("error" , err ))
0 commit comments