Skip to content

Commit e1578e9

Browse files
authored
Merge pull request #14 from ST2Projects/tk/post-prod-deploy-fixes
Tk/post prod deploy fixes
2 parents 1c6edea + 62455e1 commit e1578e9

File tree

7 files changed

+96
-18
lines changed

7 files changed

+96
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,4 @@ fabric.properties
172172
resources
173173
dist/
174174
ssh-sentinel-server
175+
prod-http-tests.http

.goreleaser.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ before:
88
- go generate ./...
99
builds:
1010
- env:
11-
- CGO_ENABLED=0
1211
goos:
1312
- linux
1413
archives:

helper/sanitizer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package helper
2+
3+
import "strings"
4+
5+
func Sanitize(s string) string {
6+
return strings.Replace(s, "\n", "", -1)
7+
}
8+
9+
func SanitizeStringSlice(ss []string) {
10+
for i, x := range ss {
11+
ss[i] = Sanitize(x)
12+
}
13+
}

helper/sanitizer_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package helper
2+
3+
import (
4+
"reflect"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestSanitize(t *testing.T) {
10+
ours := "abc"
11+
theirs := Sanitize("abc\n\n\n")
12+
13+
if ours != theirs {
14+
t.Errorf("Got %s but wanted %s", theirs, ours)
15+
}
16+
}
17+
18+
func TestSanitizeWithNoNewLines(t *testing.T) {
19+
ours := "abc"
20+
theirs := Sanitize("abc")
21+
22+
if ours != theirs {
23+
t.Errorf("Got %s but wanted %s", theirs, ours)
24+
}
25+
}
26+
27+
func TestSliceSanitize(t *testing.T) {
28+
ours := strings.Split("abc aaa def", " ")
29+
30+
theirs := strings.Split("abc aaa\n def\n\n", " ")
31+
SanitizeStringSlice(theirs)
32+
33+
if !reflect.DeepEqual(ours, theirs) {
34+
t.Errorf("Got %v but wanted %v", theirs, ours)
35+
}
36+
}
37+
38+
func TestSliceWithNoNewLinesSanitize(t *testing.T) {
39+
ours := strings.Split("abc aaa def", " ")
40+
41+
theirs := strings.Split("abc aaa def", " ")
42+
SanitizeStringSlice(theirs)
43+
44+
if !reflect.DeepEqual(ours, theirs) {
45+
t.Errorf("Got %v but wanted %v", theirs, ours)
46+
}
47+
}

server/handlers.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/st2projects/ssh-sentinel-server/crypto"
1010
"github.com/st2projects/ssh-sentinel-server/helper"
1111
"github.com/st2projects/ssh-sentinel-server/sql"
12-
"io/ioutil"
12+
"io"
1313
"net/http"
1414
"time"
1515
)
@@ -18,41 +18,56 @@ func AuthenticationHandler(next http.Handler) http.Handler {
1818
fn := func(w http.ResponseWriter, r *http.Request) {
1919
w.Header().Set(contentTypeKey, jsonContentType)
2020

21-
body, err := ioutil.ReadAll(r.Body)
21+
body, err := io.ReadAll(r.Body)
2222

2323
if err != nil {
2424
panic(helper.NewError("Failed to marshall request %s", err))
2525
}
2626

2727
signRequest, err := MarshallSigningRequest(bytes.NewReader(body))
2828

29-
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
29+
r.Body = io.NopCloser(bytes.NewBuffer(body))
3030

3131
if err != nil {
3232
panic(helper.NewError("Failed to marshall request %s", err))
3333
}
3434

35-
user := sql.GetUserByUsername(signRequest.Username)
35+
user, err := sql.GetUserByUsername(signRequest.Username)
36+
37+
if err != nil {
38+
authorisationFailed(w, "No such user %s", helper.Sanitize(signRequest.Username))
39+
}
3640

3741
hasValidAPIKey, err := crypto.Validate(signRequest.APIKey, user.APIKey.Key)
3842

3943
if !hasValidAPIKey {
40-
w.WriteHeader(http.StatusUnauthorized)
41-
panic(helper.NewError("Unauthorised key"))
44+
authorisationFailed(w, "Invalid API key for user %s", helper.Sanitize(signRequest.Username))
4245
}
4346

4447
hasValidPrincipals := CheckPrincipals(user.Principals, signRequest.Principals)
4548

4649
if !hasValidPrincipals {
47-
panic(helper.NewError("One or more unauthorised principals requested %v", signRequest.Principals))
50+
// Sanitize the principals for logging
51+
helper.SanitizeStringSlice(signRequest.Principals)
52+
authorisationFailed(w, "One or more unauthorised principals requested %v", signRequest.Principals)
4853
}
4954

55+
log.Infof("User %s is authenticated", helper.Sanitize(signRequest.Username))
56+
5057
next.ServeHTTP(w, r)
5158
}
5259

5360
return http.HandlerFunc(fn)
5461
}
5562

63+
func authorisationFailed(w http.ResponseWriter, msg string, args ...any) {
64+
w.WriteHeader(http.StatusUnauthorized)
65+
66+
log.Errorf(msg, args...)
67+
68+
panic(helper.NewError("Authentication failed"))
69+
}
70+
5671
func LoggingHandler(next http.Handler) http.Handler {
5772
fn := func(w http.ResponseWriter, r *http.Request) {
5873
t1 := time.Now()

server/server.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ func GetCAKey() (caPriv ssh.Signer) {
118118
return privKey
119119
}
120120

121-
func Version(response http.ResponseWriter, r *http.Request) {
122-
io.WriteString(response, "Version: 0.0.0.1")
121+
func Ping(response http.ResponseWriter, r *http.Request) {
122+
io.WriteString(response, fmt.Sprintf("Pong\n Time now is %s", time.Now().Format("2006-01-02 15:04:05")))
123123
}
124124

125125
func Serve(httpConfig *cmd_model.HTTPConfig) {
@@ -137,7 +137,7 @@ func Serve(httpConfig *cmd_model.HTTPConfig) {
137137
// a simple constructor for a http.Server with our Handler
138138
makeServer = func() *http.Server {
139139
return &http.Server{
140-
Addr: fmt.Sprintf(":%d", httpConfig.HttpsPort),
140+
Addr: fmt.Sprintf("0.0.0.0:%d", httpConfig.HttpsPort),
141141
Handler: makeRouter(),
142142
TLSConfig: tlsConf,
143143
}
@@ -184,10 +184,10 @@ func Serve(httpConfig *cmd_model.HTTPConfig) {
184184
}
185185

186186
// Redirect 80 -> 443
187-
go http.ListenAndServe(fmt.Sprintf(":%d", httpConfig.HttpPort), http.HandlerFunc(simplecert.Redirect))
187+
go http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", httpConfig.HttpPort), http.HandlerFunc(simplecert.Redirect))
188188

189189
tlsConf.GetCertificate = certReloader.GetCertificateFunc()
190-
log.Infof("Serving at https://%s", configuredTls.CertDomains[0])
190+
log.Infof("Serving at https://%s:%d", configuredTls.CertDomains[0], httpConfig.HttpsPort)
191191
serve(ctx, srv)
192192
<-make(chan bool)
193193
}
@@ -197,8 +197,8 @@ func makeRouter() *mux.Router {
197197

198198
router := mux.NewRouter()
199199

200-
router.HandleFunc("/", Version)
201-
router.HandleFunc("/version", Version)
200+
router.HandleFunc("/", Ping)
201+
router.HandleFunc("/ping", Ping)
202202
router.Handle("/ssh", commonHandlers.ThenFunc(KeySignHandler))
203203

204204
return router

sql/SqlDb.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sql
33
import (
44
log "github.com/sirupsen/logrus"
55
"github.com/st2projects/ssh-sentinel-server/config"
6+
"github.com/st2projects/ssh-sentinel-server/helper"
67
"github.com/st2projects/ssh-sentinel-server/model/db"
78
_ "gorm.io/driver/sqlite" // Import sqlite3 driver
89
"gorm.io/gorm"
@@ -37,10 +38,12 @@ func NewUser(user *db.User) {
3738
dbConnection.Create(user)
3839
}
3940

40-
func GetUserByUsername(username string) db.User {
41+
func GetUserByUsername(username string) (db.User, error) {
4142

4243
var user = db.User{}
43-
dbConnection.First(&user, "user_name = ? ", username)
44+
if dbc := dbConnection.First(&user, "user_name = ? ", username); dbc.Error != nil {
45+
return db.User{}, helper.NewError("No user with username %s found", username)
46+
}
4447

4548
var principals []db.Principal
4649
dbConnection.Find(&principals, "user_id = ?", user.ID)
@@ -50,7 +53,7 @@ func GetUserByUsername(username string) db.User {
5053
dbConnection.Find(&apiKey, "user_id = ?", user.ID)
5154
user.APIKey = apiKey
5255

53-
return user
56+
return user, nil
5457
}
5558

5659
func GetUserByID(id uint) db.User {

0 commit comments

Comments
 (0)