Skip to content

Commit

Permalink
Allow access for public topics
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomika committed Nov 20, 2024
1 parent 48dfb5b commit c9d7ac3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
9 changes: 3 additions & 6 deletions pipe/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

var (
cleanRegex = regexp.MustCompile(`[^0-9a-zA-Z,]`)
cleanRegex = regexp.MustCompile(`[^0-9a-zA-Z,/]`)
sshClient *pipe.Client
)

Expand Down Expand Up @@ -90,7 +90,7 @@ func handleSub(pubsub bool) http.HandlerFunc {
params += " -k"
}

id := uuid.New().String()
id := uuid.NewString()

p, err := sshClient.AddSession(id, fmt.Sprintf("sub %s %s", params, topic), 0, -1, -1)
if err != nil {
Expand Down Expand Up @@ -141,9 +141,6 @@ func handlePub(pubsub bool) http.HandlerFunc {
logger.Info("adding access list", "topic", topic, "info", clientInfo, "access", accessList)
cleanList := cleanRegex.ReplaceAllString(accessList, "")
params += fmt.Sprintf(" -a=%s", cleanList)
params = params[3:]

topic = fmt.Sprintf("web-%s", topic)
}

var wg sync.WaitGroup
Expand All @@ -163,7 +160,7 @@ func handlePub(pubsub bool) http.HandlerFunc {
params += " -e"
}

id := uuid.New().String()
id := uuid.NewString()

p, err := sshClient.AddSession(id, fmt.Sprintf("pub %s %s", params, topic), 0, -1, -1)
if err != nil {
Expand Down
48 changes: 33 additions & 15 deletions pipe/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,13 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
if *public {
name = toPublicTopic(topic)
msgFlag = "-p "
withoutUser = name
} else {
withoutUser = topic
}
}

if len(accessList) > 0 && !*public {
if len(accessList) > 0 {
_, loaded := handler.Access.LoadOrStore(name, accessList)
if !loaded {
defer func() {
Expand All @@ -378,6 +379,17 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
}
}

if accessList, ok := handler.Access.Load(withoutUser); !isAdmin && ok {
if checkAccess(accessList, userName, sesh) {
name = withoutUser
} else if !*public {
name = toTopic(userName, withoutUser)
} else {
topic = uuid.NewString()
name = toPublicTopic(topic)
}
}

if !*clean {
wish.Printf(
sesh,
Expand All @@ -388,12 +400,6 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
)
}

if accessList, ok := handler.Access.Load(withoutUser); !isAdmin && !*public && ok {
if checkAccess(accessList, userName, sesh) {
name = withoutUser
}
}

var pubCtx context.Context = pipeCtx

if *block {
Expand Down Expand Up @@ -546,14 +552,20 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
name = toTopic(userName, topic)
if *public {
name = toPublicTopic(topic)
withoutUser = name
} else {
withoutUser = topic
}
}

if accessList, ok := handler.Access.Load(withoutUser); !isAdmin && !*public && ok {
if accessList, ok := handler.Access.Load(withoutUser); !isAdmin && ok {
if checkAccess(accessList, userName, sesh) {
name = withoutUser
} else if !*public {
name = toTopic(userName, withoutUser)
} else {
wish.Errorln(sesh, "access denied")
return
}
}

Expand Down Expand Up @@ -617,12 +629,13 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
if *public {
name = toPublicTopic(topic)
flagMsg = "-p "
withoutUser = name
} else {
withoutUser = topic
}
}

if len(accessList) > 0 && !*public {
if len(accessList) > 0 {
_, loaded := handler.Access.LoadOrStore(name, accessList)
if !loaded {
defer func() {
Expand All @@ -631,6 +644,17 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
}
}

if accessList, ok := handler.Access.Load(withoutUser); !isAdmin && ok {
if checkAccess(accessList, userName, sesh) {
name = withoutUser
} else if !*public {
name = toTopic(userName, withoutUser)
} else {
topic = uuid.NewString()
name = toPublicTopic(topic)
}
}

if isCreator && !*clean {
wish.Printf(
sesh,
Expand All @@ -641,12 +665,6 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
)
}

if accessList, ok := handler.Access.Load(withoutUser); !isAdmin && !*public && ok {
if checkAccess(accessList, userName, sesh) {
name = withoutUser
}
}

readErr, writeErr := pubsub.Pipe(
pipeCtx,
clientID,
Expand Down

0 comments on commit c9d7ac3

Please sign in to comment.