-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
325 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package cmd | |
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mogenius/punq/dtos" | ||
"github.com/mogenius/punq/services" | ||
|
@@ -31,25 +32,42 @@ var addUserCmd = &cobra.Command{ | |
Short: "Add punq user.", | ||
Long: `The add command lets you add a user into punq.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
RequireStringFlag(email, "email") | ||
RequireStringFlag(displayName, "displayname") | ||
RequireStringFlag(password, "password") | ||
|
||
selectedAccess := dtos.READER // default level | ||
if accessLevel != "" { | ||
selectedAccess = dtos.AccessLevelFromString(accessLevel) | ||
} else { | ||
selectedAccess = dtos.ADMIN | ||
} | ||
|
||
firstname := utils.RandomFirstName() | ||
middlename := utils.RandomMiddleName() | ||
lastname := utils.RandomLastName() | ||
|
||
if email == "" { | ||
email = fmt.Sprintf("%s-%[email protected]", strings.ToLower(firstname), strings.ToLower(lastname)) | ||
} | ||
|
||
_, err := services.AddUser(dtos.PunqUserCreateInput{ | ||
if password == "" { | ||
password = utils.NanoId() | ||
} | ||
|
||
if displayName == "" { | ||
displayName = fmt.Sprintf("%s %s %s", firstname, middlename, lastname) | ||
} | ||
|
||
newUser := dtos.PunqUserCreateInput{ | ||
Email: email, | ||
Password: password, | ||
DisplayName: displayName, | ||
AccessLevel: selectedAccess, | ||
}) | ||
} | ||
|
||
_, err := services.AddUser(newUser) | ||
if err != nil { | ||
utils.FatalError(err.Error()) | ||
} else { | ||
utils.PrintInfo("User added succesfully ✅.") | ||
structs.PrettyPrint(newUser) | ||
} | ||
}, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package operator | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gorilla/websocket" | ||
"github.com/mogenius/punq/dtos" | ||
"github.com/mogenius/punq/services" | ||
"github.com/mogenius/punq/utils" | ||
) | ||
|
||
func InitWebsocketRoutes(router *gin.Engine) { | ||
router.GET("/exec-sh", Auth(dtos.ADMIN), RequireNamespace(), RequirePodName(), RequireContainerName(), connectWs) | ||
} | ||
|
||
var upgrader = websocket.Upgrader{ | ||
ReadBufferSize: 1024, | ||
WriteBufferSize: 1024, | ||
CheckOrigin: func(r *http.Request) bool { | ||
return true // adjust to implement your origin validation logic | ||
}, | ||
} | ||
|
||
func connectWs(c *gin.Context) { | ||
namespace := services.GetGinNamespace(c) | ||
container := services.GetGinContainername(c) | ||
podName := services.GetGinPodname(c) | ||
|
||
log.Printf("exec-sh: %s %s %s\n", *namespace, *container, *podName) | ||
|
||
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) | ||
if err != nil { | ||
log.Printf("Failed to upgrade ws: %+v", err) | ||
return | ||
} | ||
defer func() { | ||
ws.Close() | ||
}() | ||
|
||
cmd := exec.Command("sh", "-c", fmt.Sprintf("kubectl exec -i --tty -c %s -n %s %s -- /bin/sh", *container, *namespace, *podName)) | ||
cmd.Env = os.Environ() | ||
stdin, err := cmd.StdinPipe() | ||
if err != nil { | ||
log.Fatal("Error creating stdin pipe:", err) | ||
} | ||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
log.Fatal("Error creating stdout pipe:", err) | ||
} | ||
|
||
go func() { | ||
scanner := bufio.NewScanner(stdout) | ||
for scanner.Scan() { | ||
data := scanner.Bytes() | ||
if utils.CONFIG.Misc.Debug { | ||
fmt.Printf("Response-Line: '%s'\n", string(data)) | ||
} | ||
err = ws.WriteMessage(websocket.TextMessage, data) | ||
if err != nil { | ||
log.Printf("Error writing to ws: %+v", err) | ||
} | ||
} | ||
}() | ||
|
||
go func() { | ||
for { | ||
_, msg, err := ws.ReadMessage() | ||
if utils.CONFIG.Misc.Debug { | ||
fmt.Printf("Received Cmd: '%s'", string(msg)) | ||
} | ||
msg = append(msg, '\n') | ||
if err != nil { | ||
log.Printf("Error reading from ws: %+v", err) | ||
log.Printf("CLOSE: exec-sh: %s %s %s\n", *namespace, *container, *podName) | ||
break | ||
} | ||
_, err = stdin.Write(msg) | ||
if err != nil { | ||
log.Printf("Error writing to stdin: %+v", err) | ||
} | ||
} | ||
}() | ||
|
||
err = cmd.Start() | ||
if err != nil { | ||
log.Printf("Error starting cmd: %+v", err) | ||
return | ||
} | ||
|
||
err = cmd.Wait() | ||
if err != nil { | ||
log.Printf("Cmd returned error: %+v", err.Error()) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.