Skip to content

Commit 9aaf925

Browse files
authored
Merge pull request #118 from ST2Projects/tk/combine-seperate-projects
Tk/combine seperate projects
2 parents 8ce2d05 + 2174c2e commit 9aaf925

26 files changed

+390
-64
lines changed

.github/workflows/go.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ jobs:
1717
uses: actions/setup-go@v4
1818
with:
1919
go-version: 1.22
20+
2021
- name: Test
2122
run: go test -v ./...
2223

24+
- name: Create output dir
25+
run: mkdir -p "dist/bin"
26+
2327
- name: Build
24-
run: go build -v
28+
run: go build -v -o dist/bin ./...

.github/workflows/release-package.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ jobs:
2222
go-version: 1.22
2323
- name: Test
2424
run: go test -v ./...
25+
- name: Create output dir
26+
run: mkdir -p "dist/bin"
2527
- name: Build
26-
run: go build -v
28+
run: go build -v -o dist/bin ./...
2729
- name: Setup BuildX
2830
uses: docker/setup-buildx-action@v3
2931
- name: Login to registy ${{ env.REGISTRY }}

.goreleaser.yml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
11
env_files:
22
github_token: .githubtoken
3+
34
before:
45
hooks:
56
- go mod tidy
7+
68
builds:
7-
- goarch:
9+
- id: ssh-sentinel-server
10+
main: ./cmd/ssh-sentinel-server/main.go
11+
binary: ssh-sentinel-server
12+
goarch:
13+
- amd64
14+
goos:
15+
- linux
16+
- id: ssh-sentinel-client
17+
main: ./cmd/ssh-sentinel-client/main.go
18+
binary: ssh-sentinel-client
19+
goarch:
820
- amd64
921
goos:
1022
- linux
1123

1224
archives:
13-
- files:
25+
- id: ssh-sentinel-server
26+
builds:
27+
- ssh-sentinel-server
28+
files:
1429
- samples
1530
- README.md
1631
- LICENSE
32+
name_template: &name_template
33+
'{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}'
34+
- id: ssh-sentinel-client
35+
builds:
36+
- ssh-sentinel-client
37+
files:
38+
- README.md
39+
- LICENSE
40+
name_template: *name_template
41+
1742
checksum:
1843
name_template: 'checksums.txt'
44+
1945
snapshot:
2046
name_template: "{{ .Tag }}-next"
47+
2148
changelog:
2249
sort: asc
2350
filters:
@@ -33,4 +60,4 @@ signs:
3360
artifacts: checksum
3461
release:
3562
prerelease: auto
36-
mode: keep-existing
63+
mode: keep-existing

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ FROM golang:1.22
33
LABEL authors="st2projects"
44
LABEL org.opencontainers.image.source=https://github.com/st2projects/ssh-sentinel-server
55

6-
COPY ssh-sentinel-server ./
6+
COPY dist/bin/ssh-sentinel-server ./
7+
COPY dist/bin/ssh-sentinel-client ./
78

89
EXPOSE 8080
910
RUN ["mkdir", "/resources"]

Makefile

Lines changed: 0 additions & 17 deletions
This file was deleted.

cli/client/clientConfigReader.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package clientcli
2+
3+
import (
4+
"encoding/json"
5+
"github.com/st2projects/ssh-sentinel-server/model/api"
6+
"os"
7+
)
8+
9+
type ClientConfigType struct {
10+
EndPoint string `json:"endPoint"`
11+
APIKey string `json:"apiKey"`
12+
Username string `json:"username"`
13+
Principals []string `json:"principals"`
14+
Extensions []api.Extension `json:"extensions"`
15+
PublicKey string `json:"publicKey"`
16+
CertFile string `json:"certFile"`
17+
}
18+
19+
var Config *ClientConfigType
20+
21+
func MakeConfig(configFile string) {
22+
if !PathExists(configFile) {
23+
panic("config file " + configFile + " does not exits")
24+
}
25+
26+
configString, err := os.ReadFile(configFile)
27+
if err != nil {
28+
panic(err)
29+
}
30+
31+
err = json.Unmarshal(configString, &Config)
32+
if err != nil {
33+
panic(err)
34+
}
35+
36+
}
37+
38+
func (c *ClientConfigType) GetPublicKey() string {
39+
return ExpandPath(c.PublicKey)
40+
}
41+
42+
func (c *ClientConfigType) GetCertFile() string {
43+
return ExpandPath(c.CertFile)
44+
}

cli/client/init.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package clientcli
2+
3+
import (
4+
"encoding/json"
5+
"github.com/spf13/cobra"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
var initCmd = &cobra.Command{
11+
Use: "init",
12+
Short: "Initialise the client",
13+
Run: func(cmd *cobra.Command, args []string) {
14+
15+
configBytes, err := json.MarshalIndent(&ClientConfigType{}, "", " ")
16+
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
userHome, err := os.UserHomeDir()
22+
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
configPath := filepath.Join(userHome, ".ssh-sentinel.json")
28+
err = os.WriteFile(configPath, configBytes, os.FileMode(0600))
29+
if err != nil {
30+
panic(err)
31+
}
32+
},
33+
}
34+
35+
func init() {
36+
rootCmd.AddCommand(initCmd)
37+
}

cli/client/keyHelper.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package clientcli
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
"golang.org/x/crypto/ssh"
6+
"os"
7+
"time"
8+
)
9+
10+
func IsCertValid(certPath string) (bool, string) {
11+
12+
certValid := PathExists(certPath)
13+
14+
if certValid {
15+
certBytes, err := os.ReadFile(certPath)
16+
if err != nil {
17+
log.Errorf("%s - cert does not exist or cannot be read", certPath)
18+
}
19+
20+
pub, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
21+
if err != nil {
22+
log.Errorf("Error when parsing cert: %s", err.Error())
23+
}
24+
25+
cert, ok := pub.(*ssh.Certificate)
26+
27+
if !ok {
28+
log.Errorf("Failed to cast to cert")
29+
}
30+
31+
now := time.Now().UTC()
32+
validBefore := time.Unix(int64(cert.ValidBefore), 0).UTC()
33+
validAfter := time.Unix(int64(cert.ValidAfter), 0).UTC()
34+
35+
validBeforeString := validBefore.Format("2006-01-02 15:04:05.5 UTC")
36+
37+
return now.After(validAfter) && now.Before(validBefore), validBeforeString
38+
}
39+
40+
return false, "Cert not found"
41+
}

cli/client/pathHelper.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package clientcli
2+
3+
import (
4+
"errors"
5+
"os"
6+
"os/user"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
func ExpandPath(path string) string {
12+
usr, _ := user.Current()
13+
homeDir := usr.HomeDir
14+
15+
if path == "~" {
16+
path = homeDir
17+
} else if strings.HasPrefix(path, "~/") {
18+
path = filepath.Join(homeDir, path[2:])
19+
}
20+
21+
return path
22+
}
23+
24+
func PathExists(path string) bool {
25+
_, err := os.Stat(path)
26+
27+
return !(err != nil && errors.Is(err, os.ErrNotExist))
28+
}

cli/client/root.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package clientcli
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"os"
6+
)
7+
8+
var rootCmd = &cobra.Command{
9+
Use: "ssh-sentinel-client <ARGS>",
10+
Short: "A simple ssh-sentinel client",
11+
}
12+
13+
func Execute() {
14+
err := rootCmd.Execute()
15+
16+
if err != nil {
17+
os.Exit(1)
18+
}
19+
}

0 commit comments

Comments
 (0)