Skip to content

Commit

Permalink
rest server
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Oct 15, 2024
1 parent 486df94 commit c08f1c5
Show file tree
Hide file tree
Showing 41 changed files with 3,967 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/pkg/rest/static/
2 changes: 1 addition & 1 deletion auth/provisioning/scim/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
)

func (s *scim) authMiddleware(next http.Handler) http.Handler {
func (s *Scim) authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.Header.Get("Authorization"), "Bearer ") {
writeWithStatus(w, []byte(`{"error": "token not found"}`), http.StatusUnauthorized)
Expand Down
4 changes: 2 additions & 2 deletions auth/provisioning/scim/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/in4it/go-devops-platform/users"
)

func New(storage storage.Iface, userStore *users.UserStore, token string, disableFunc DisableFunc, reactivateFunc ReactivateFunc) *scim {
s := &scim{
func New(storage storage.Iface, userStore *users.UserStore, token string, disableFunc DisableFunc, reactivateFunc ReactivateFunc) *Scim {
s := &Scim{
Token: token,
UserStore: userStore,
storage: storage,
Expand Down
2 changes: 1 addition & 1 deletion auth/provisioning/scim/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
)

func (s *scim) GetRouter() *http.ServeMux {
func (s *Scim) GetRouter() *http.ServeMux {
mux := http.NewServeMux()

mux.Handle("/api/scim/", s.authMiddleware(http.HandlerFunc(notFoundHandler)))
Expand Down
2 changes: 1 addition & 1 deletion auth/provisioning/scim/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type DisableFunc func(storage.Iface, users.User) error
type ReactivateFunc func(storage.Iface, users.User) error

type scim struct {
type Scim struct {
Token string `json:"token"`
UserStore *users.UserStore `json:"userStore"`
storage storage.Iface
Expand Down
2 changes: 1 addition & 1 deletion auth/provisioning/scim/update.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package scim

func (s *scim) UpdateToken(token string) {
func (s *Scim) UpdateToken(token string) {
s.Token = token
}
14 changes: 7 additions & 7 deletions auth/provisioning/scim/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// handler for multiple users
func (s *scim) usersHandler(w http.ResponseWriter, r *http.Request) {
func (s *Scim) usersHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
s.GetUsersHandler(w, r)
Expand All @@ -25,7 +25,7 @@ func (s *scim) usersHandler(w http.ResponseWriter, r *http.Request) {
}

// handler for a single user
func (s *scim) userHandler(w http.ResponseWriter, r *http.Request) {
func (s *Scim) userHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
s.GetUsersHandler(w, r)
Expand All @@ -41,7 +41,7 @@ func (s *scim) userHandler(w http.ResponseWriter, r *http.Request) {
}
}

func (s *scim) GetUsersHandler(w http.ResponseWriter, r *http.Request) {
func (s *Scim) GetUsersHandler(w http.ResponseWriter, r *http.Request) {
attributes := r.URL.Query().Get("attributes")
filter := r.URL.Query().Get("filter")
count, err := strconv.Atoi(r.URL.Query().Get("count"))
Expand Down Expand Up @@ -70,7 +70,7 @@ func (s *scim) GetUsersHandler(w http.ResponseWriter, r *http.Request) {
write(w, response)
}

func (s *scim) getUserHandler(w http.ResponseWriter, r *http.Request) {
func (s *Scim) getUserHandler(w http.ResponseWriter, r *http.Request) {
user, err := s.UserStore.GetUserByID(r.PathValue("id"))
if err != nil {
returnError(w, fmt.Errorf("get user by id error: %s", err), http.StatusBadRequest)
Expand All @@ -85,7 +85,7 @@ func (s *scim) getUserHandler(w http.ResponseWriter, r *http.Request) {

write(w, response)
}
func (s *scim) PutUserHandler(w http.ResponseWriter, r *http.Request) {
func (s *Scim) PutUserHandler(w http.ResponseWriter, r *http.Request) {
user, err := s.UserStore.GetUserByID(r.PathValue("id"))
if err != nil {
returnError(w, fmt.Errorf("get user by id error: %s", err), http.StatusBadRequest)
Expand Down Expand Up @@ -137,7 +137,7 @@ func (s *scim) PutUserHandler(w http.ResponseWriter, r *http.Request) {
write(w, response)
}

func (s *scim) DeleteUserHandler(w http.ResponseWriter, r *http.Request) {
func (s *Scim) DeleteUserHandler(w http.ResponseWriter, r *http.Request) {
user, err := s.UserStore.GetUserByID(r.PathValue("id"))
if err != nil {
returnError(w, fmt.Errorf("get user by id error: %s", err), http.StatusBadRequest)
Expand All @@ -159,7 +159,7 @@ func (s *scim) DeleteUserHandler(w http.ResponseWriter, r *http.Request) {
write(w, []byte(""))
}

func (s *scim) PostUsersHandler(w http.ResponseWriter, r *http.Request) {
func (s *Scim) PostUsersHandler(w http.ResponseWriter, r *http.Request) {
var postUserRequest PostUserRequest
err := json.NewDecoder(r.Body).Decode(&postUserRequest)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ require (
golang.org/x/crypto v0.28.0
)

require (
golang.org/x/net v0.21.0 // indirect
golang.org/x/text v0.19.0 // indirect
)

require (
github.com/aws/aws-sdk-go-v2 v1.32.2
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.6 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
39 changes: 39 additions & 0 deletions rest/auditlog/logentry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package auditlog

import (
"encoding/json"
"fmt"
"path"
"time"

"github.com/in4it/go-devops-platform/storage"
)

const TIMESTAMP_FORMAT = "2006-01-02T15:04:05"
const AUDITLOG_STATS_DIR = "stats"

type LogEntry struct {
Timestamp LogTimestamp `json:"timestamp"`
UserID string `json:"userID"`
Action string `json:"action"`
}
type LogTimestamp time.Time

func (t LogTimestamp) MarshalJSON() ([]byte, error) {
timestamp := fmt.Sprintf("\"%s\"", time.Time(t).Format(TIMESTAMP_FORMAT))
return []byte(timestamp), nil
}

func Write(storage storage.Iface, logEntry LogEntry) error {
statsPath := path.Join(AUDITLOG_STATS_DIR, "logins-"+time.Now().Format("2006-01-02")) + ".log"
logEntryBytes, err := json.Marshal(logEntry)
if err != nil {
return fmt.Errorf("could not parse log entry: %s", err)
}
err = storage.AppendFile(statsPath, logEntryBytes)
if err != nil {
return fmt.Errorf("could not append stats to file (%s): %s", statsPath, err)
}

return nil
}
Loading

0 comments on commit c08f1c5

Please sign in to comment.