Skip to content

Commit 1ac0786

Browse files
committed
Added post deploy fixes
1 parent 2778682 commit 1ac0786

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

.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:

server/handlers.go

Lines changed: 20 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,54 @@ 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", 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", 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+
authorisationFailed(w, "One or more unauthorised principals requested %v", signRequest.Principals)
4851
}
4952

53+
log.Infof("User %s is authenticated", signRequest.Username)
54+
5055
next.ServeHTTP(w, r)
5156
}
5257

5358
return http.HandlerFunc(fn)
5459
}
5560

61+
func authorisationFailed(w http.ResponseWriter, msg string, args ...any) {
62+
w.WriteHeader(http.StatusUnauthorized)
63+
64+
log.Errorf(msg, args)
65+
66+
panic(helper.NewError("Authentication failed"))
67+
}
68+
5669
func LoggingHandler(next http.Handler) http.Handler {
5770
fn := func(w http.ResponseWriter, r *http.Request) {
5871
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)