repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

commit
c46187d
parent
1dcd955
author
Antonio Mika
date
2025-03-15 15:05:42 -0400 EDT
Add impersonation for pipe
1 files changed,  +21, -5
M pkg/apps/pipe/cli.go
+21, -5
 1@@ -56,7 +56,7 @@ func toPublicTopic(topic string) string {
 2 	return fmt.Sprintf("public/%s", topic)
 3 }
 4 
 5-func clientInfo(clients []*psub.Client, clientType string) string {
 6+func clientInfo(clients []*psub.Client, isAdmin bool, clientType string) string {
 7 	if len(clients) == 0 {
 8 		return ""
 9 	}
10@@ -64,6 +64,10 @@ func clientInfo(clients []*psub.Client, clientType string) string {
11 	outputData := fmt.Sprintf("    %s:\r\n", clientType)
12 
13 	for _, client := range clients {
14+		if strings.HasPrefix(client.ID, "admin-") && !isAdmin {
15+			continue
16+		}
17+
18 		outputData += fmt.Sprintf("    - %s\r\n", client.ID)
19 	}
20 
21@@ -161,8 +165,12 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware {
22 			userNameAddition := ""
23 
24 			isAdmin := false
25+			impersonate := false
26 			if user != nil {
27 				isAdmin = handler.DBPool.HasFeatureForUser(user.ID, "admin")
28+				if isAdmin && strings.HasPrefix(sesh.User(), "admin__") {
29+					impersonate = true
30+				}
31 
32 				userName = user.Name
33 				if user.PublicKey != nil && user.PublicKey.Name != "" {
34@@ -254,9 +262,9 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware {
35 									pipes = append(pipes, client)
36 								}
37 							}
38-							outputData += clientInfo(pubs, "Pubs")
39-							outputData += clientInfo(subs, "Subs")
40-							outputData += clientInfo(pipes, "Pipes")
41+							outputData += clientInfo(pubs, isAdmin, "Pubs")
42+							outputData += clientInfo(subs, isAdmin, "Subs")
43+							outputData += clientInfo(pipes, isAdmin, "Pipes")
44 						}
45 
46 						for waitingChannel, channelPubs := range waitingChannels {
47@@ -270,6 +278,9 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware {
48 							outputData += "  Clients:\r\n"
49 							outputData += fmt.Sprintf("    %s:\r\n", "Waiting Pubs")
50 							for _, client := range channelPubs {
51+								if strings.HasPrefix(client, "admin-") && !isAdmin {
52+									continue
53+								}
54 								outputData += fmt.Sprintf("    - %s\r\n", client)
55 							}
56 						}
57@@ -296,7 +307,12 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware {
58 				"cmdArgs", cmdArgs,
59 			)
60 
61-			clientID := fmt.Sprintf("%s (%s%s@%s)", uuid.NewString(), userName, userNameAddition, sesh.RemoteAddr().String())
62+			uuidStr := uuid.NewString()
63+			if impersonate {
64+				uuidStr = fmt.Sprintf("admin-%s", uuidStr)
65+			}
66+
67+			clientID := fmt.Sprintf("%s (%s%s@%s)", uuidStr, userName, userNameAddition, sesh.RemoteAddr().String())
68 
69 			var err error
70