Skip to content

Commit 8411d31

Browse files
committed
unify code-paths
1 parent 1f38a8b commit 8411d31

File tree

2 files changed

+39
-44
lines changed

2 files changed

+39
-44
lines changed

coderd/agentapi/audit.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,13 @@ func (a *AuditAPI) ReportConnection(ctx context.Context, req *agentproto.ReportC
3434
return nil, xerrors.Errorf("connection id from bytes: %w", err)
3535
}
3636

37-
var action database.AuditAction
38-
switch req.GetConnection().GetAction() {
39-
case agentproto.Connection_CONNECT:
40-
action = database.AuditActionConnect
41-
case agentproto.Connection_DISCONNECT:
42-
action = database.AuditActionDisconnect
43-
default:
44-
return nil, xerrors.Errorf("unknown agent connection action %q", req.GetConnection().GetAction())
37+
action, err := AgentProtoConnectionActionToAuditAction(req.GetConnection().GetAction())
38+
if err != nil {
39+
return nil, err
4540
}
46-
47-
var connectionType agentsdk.ConnectionType
48-
switch req.GetConnection().GetType() {
49-
case agentproto.Connection_SSH:
50-
connectionType = agentsdk.ConnectionTypeSSH
51-
case agentproto.Connection_VSCODE:
52-
connectionType = agentsdk.ConnectionTypeVSCode
53-
case agentproto.Connection_JETBRAINS:
54-
connectionType = agentsdk.ConnectionTypeJetBrains
55-
case agentproto.Connection_RECONNECTING_PTY:
56-
connectionType = agentsdk.ConnectionTypeReconnectingPTY
57-
default:
58-
return nil, xerrors.Errorf("unknown agent connection type %q", req.GetConnection().GetType())
41+
connectionType, err := AgentProtoConnectionTypeToAgentConnectionType(req.GetConnection().GetType())
42+
if err != nil {
43+
return nil, err
5944
}
6045

6146
// Fetch contextual data for this audit event.
@@ -115,3 +100,29 @@ func (a *AuditAPI) ReportConnection(ctx context.Context, req *agentproto.ReportC
115100

116101
return &emptypb.Empty{}, nil
117102
}
103+
104+
func AgentProtoConnectionActionToAuditAction(action agentproto.Connection_Action) (database.AuditAction, error) {
105+
switch action {
106+
case agentproto.Connection_CONNECT:
107+
return database.AuditActionConnect, nil
108+
case agentproto.Connection_DISCONNECT:
109+
return database.AuditActionDisconnect, nil
110+
default:
111+
return "", xerrors.Errorf("unknown agent connection action %q", action)
112+
}
113+
}
114+
115+
func AgentProtoConnectionTypeToAgentConnectionType(typ agentproto.Connection_Type) (agentsdk.ConnectionType, error) {
116+
switch typ {
117+
case agentproto.Connection_SSH:
118+
return agentsdk.ConnectionTypeSSH, nil
119+
case agentproto.Connection_VSCODE:
120+
return agentsdk.ConnectionTypeVSCode, nil
121+
case agentproto.Connection_JETBRAINS:
122+
return agentsdk.ConnectionTypeJetBrains, nil
123+
case agentproto.Connection_RECONNECTING_PTY:
124+
return agentsdk.ConnectionTypeReconnectingPTY, nil
125+
default:
126+
return "", xerrors.Errorf("unknown agent connection type %q", typ)
127+
}
128+
}

coderd/agentapi/audit_test.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,32 +147,16 @@ func TestAuditReport(t *testing.T) {
147147
}
148148
}
149149

150-
func agentProtoConnectionActionToAudit(t *testing.T, typ agentproto.Connection_Action) database.AuditAction {
151-
switch typ {
152-
case agentproto.Connection_CONNECT:
153-
return database.AuditActionConnect
154-
case agentproto.Connection_DISCONNECT:
155-
return database.AuditActionDisconnect
156-
default:
157-
t.Fatalf("unknown agent connection action %q", typ)
158-
return ""
159-
}
150+
func agentProtoConnectionActionToAudit(t *testing.T, action agentproto.Connection_Action) database.AuditAction {
151+
a, err := agentapi.AgentProtoConnectionActionToAuditAction(action)
152+
require.NoError(t, err)
153+
return a
160154
}
161155

162156
func agentProtoConnectionTypeToSDK(t *testing.T, typ agentproto.Connection_Type) agentsdk.ConnectionType {
163-
switch typ {
164-
case agentproto.Connection_SSH:
165-
return agentsdk.ConnectionTypeSSH
166-
case agentproto.Connection_VSCODE:
167-
return agentsdk.ConnectionTypeVSCode
168-
case agentproto.Connection_JETBRAINS:
169-
return agentsdk.ConnectionTypeJetBrains
170-
case agentproto.Connection_RECONNECTING_PTY:
171-
return agentsdk.ConnectionTypeReconnectingPTY
172-
default:
173-
t.Fatalf("unknown agent connection type %q", typ)
174-
return ""
175-
}
157+
action, err := agentapi.AgentProtoConnectionTypeToAgentConnectionType(typ)
158+
require.NoError(t, err)
159+
return action
176160
}
177161

178162
func asAtomicPointer[T any](v T) *atomic.Pointer[T] {

0 commit comments

Comments
 (0)