Skip to content

Commit

Permalink
Merge pull request #26 from HilkopterBob/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
HilkopterBob authored Aug 19, 2024
2 parents 4077892 + 0187cdd commit c0b1c9a
Show file tree
Hide file tree
Showing 14 changed files with 604 additions and 172 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
certs/**
6 changes: 3 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
general:
debug: true
production: false
production: true
network:
forcehttp: true
fqdn: 0.0.0.0
port: 8080
ssl:
allowselfsigned: true
certificatepath: /etc/packagelock/ssl/cert.pem
privatekeypath: /etc/packagelock/ssl/privkey.pem
certificatepath: ./certs/testing.crt
privatekeypath: ./certs/testing.key
3 changes: 3 additions & 0 deletions config/conf-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/spf13/viper"
)

var Config ConfigProvider

type ConfigProvider interface {
SetConfigName(name string)
SetConfigType(fileext string)
Expand All @@ -21,6 +23,7 @@ type ConfigProvider interface {
AllSettings() map[string]any
GetString(string string) string
SetDefault(key string, value any)
Get(key string) any
}

// TODO: How to test?
Expand Down
45 changes: 18 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,45 @@ go 1.22.6

require (
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-gonic/gin v1.10.0
github.com/gofiber/fiber/v2 v2.52.5
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/bytedance/sonic v1.12.1 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/MicahParks/keyfunc/v2 v2.1.0 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/gofiber/contrib/jwt v1.0.10 // indirect
github.com/gofiber/jwt/v3 v3.3.10 // indirect
github.com/gofiber/template v1.8.3 // indirect
github.com/gofiber/template/html/v2 v2.1.2 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.55.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.9.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
172 changes: 115 additions & 57 deletions go.sum

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions handler/UserGroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package handler

import (
"log"
"os"
"packagelock/config"
"packagelock/structs"
"time"

"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
)

func LoginHandler(c *fiber.Ctx) error {

Check failure on line 14 in handler/UserGroup.go

View workflow job for this annotation

GitHub Actions / lint

undefined: fiber (typecheck)
type LoginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}

var loginReq LoginRequest
if err := c.BodyParser(&loginReq); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Failed to parse request",
})
}

var user structs.User
// Find the user by username
for _, u := range Users {
if u.Username == loginReq.Username {
user = u
break
}
}

// As 'user' is a struct, check for a must-have value (USerID)
// If UserID == "" the user couldn't be found -> doesn't exist!
if user.UserID == "" {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"error": "Invalid username or password",
})
}

// Validate the password
if user.Password != loginReq.Password {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"error": "Invalid username or password",
})
}

// Generate JWT token
token := jwt.New(jwt.SigningMethodRS256)

Check failure on line 52 in handler/UserGroup.go

View workflow job for this annotation

GitHub Actions / lint

undefined: jwt (typecheck)
claims := token.Claims.(jwt.MapClaims)
claims["username"] = user.Username
claims["userID"] = user.UserID
claims["exp"] = time.Now().Add(time.Hour * 72).Unix() // 3 days expiry

// Sign and get the encoded token
keyData, err := os.ReadFile(config.Config.GetString("network.ssl.privatekeypath"))
if err != nil {
log.Fatal(err)
}

privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(keyData)

Check failure on line 64 in handler/UserGroup.go

View workflow job for this annotation

GitHub Actions / lint

undefined: jwt (typecheck)
if err != nil {
log.Fatal(err)
}

tokenString, err := token.SignedString(privateKey)
if err != nil {
log.Println("Failed to generate JWT token:", err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Failed to generate token",
})
}

// Add the token to the user's APIToken slice
user.APIToken = append(user.APIToken, tokenString)

// Return the token and user information
return c.JSON(fiber.Map{
"message": "Login successful",
"token": tokenString,
"username": user.Username,
})
}
61 changes: 35 additions & 26 deletions handler/agentgroup.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,70 @@
package handler

import (
"net/http"
"packagelock/structs"
"strconv"

"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
)

// GetAgentByID filters a slice of Agents for a matching Agent.Agent_ID.
// It returns IndentedJSON with: http.StatusOK or http.StatusNotFound.
func GetAgentByID(c *gin.Context) {
id := c.Param("id")
// It returns a JSON response with fiber.StatusOK or fiber.StatusNotFound.
func GetAgentByID(c *fiber.Ctx) error {

Check failure on line 12 in handler/agentgroup.go

View workflow job for this annotation

GitHub Actions / lint

undefined: fiber (typecheck)
id := c.Params("id")

for _, a := range Agents {
if strconv.Itoa(a.Host_ID) == id {
c.IndentedJSON(http.StatusOK, a)
return
return c.Status(fiber.StatusOK).JSON(a)
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "no agent under that id"})
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"message": "no agent under that id"})
}

// POST Functions
func RegisterAgent(c *gin.Context) {
// RegisterAgent handles POST requests to register a new agent.
func RegisterAgent(c *fiber.Ctx) error {

Check failure on line 24 in handler/agentgroup.go

View workflow job for this annotation

GitHub Actions / lint

undefined: fiber (typecheck)
var newAgent structs.Agent

if err := c.BindJSON(&newAgent); err != nil {
// Parse the JSON request body into newAgent
if err := c.BodyParser(&newAgent); err != nil {
// TODO: Add logs
// TODO: Add errorhandling
return
// TODO: Add error handling
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Cannot parse JSON",
})
}

// Add new agent to the Agents slice
Agents = append(Agents, newAgent)
c.IndentedJSON(http.StatusCreated, newAgent)

// Respond with the newly created agent
return c.Status(fiber.StatusCreated).JSON(newAgent)
}

func GetHostByAgentID(c *gin.Context) {
var agent_by_id structs.Agent
// GetHostByAgentID finds the host for a given agent ID.
func GetHostByAgentID(c *fiber.Ctx) error {
var agentByID structs.Agent

// gets the value from /agent/:id/host
id := c.Param("id")
// Get the value from /agent/:id/host
id := c.Params("id")

// finds the agent by the URL-ID
// Find the agent by the URL-ID
for _, a := range Agents {
if strconv.Itoa(a.Host_ID) == id {
// c.IndentedJSON(http.StatusOK, a)
agent_by_id = a
agentByID = a
break
}
}

// finds host with same id as agent
if agentByID.Agent_ID == 0 {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"message": "no agent under that id"})
}

// Find the host with the same id as the agent
for _, host := range Hosts {
if host.ID == agent_by_id.Agent_ID {
c.IndentedJSON(http.StatusOK, host)
return
if host.ID == agentByID.Agent_ID {
return c.Status(fiber.StatusOK).JSON(host)
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "no agent under that id"})

return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"message": "no host found for that agent"})
}
14 changes: 7 additions & 7 deletions handler/generalgroup.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package handler

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
)

func GetHosts(c *gin.Context) {
c.IndentedJSON(http.StatusOK, Hosts)
// GetHosts responds with a list of all hosts.
func GetHosts(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(Hosts)
}

func GetAgents(c *gin.Context) {
c.IndentedJSON(http.StatusOK, Agents)
// GetAgents responds with a list of all agents.
func GetAgents(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(Agents)
}
20 changes: 13 additions & 7 deletions handler/hostgroup.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package handler

import (
"net/http"
"packagelock/structs"

"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
)

func RegisterHost(c *gin.Context) {
// RegisterHost handles the registration of a new host.
func RegisterHost(c *fiber.Ctx) error {
var newHost structs.Host

if err := c.BindJSON(&newHost); err != nil {
// Parse the JSON request body into newHost
if err := c.BodyParser(&newHost); err != nil {
// TODO: Add logs
// TODO: Add errorhandling
return
// TODO: Add error handling
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Cannot parse JSON",
})
}

// Append new host to the Hosts slice
Hosts = append(Hosts, newHost)
c.IndentedJSON(http.StatusCreated, newHost)

// Respond with the newly created host
return c.Status(fiber.StatusCreated).JSON(newHost)
}
5 changes: 5 additions & 0 deletions handler/synth_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ var Agents = []structs.Agent{
{Agent_name: "Agent Host2", Agent_secret: "11:11:11:12", Host_ID: 2, Agent_ID: 2},
{Agent_name: "Agent Host3", Agent_secret: "11:11:11:13", Host_ID: 3, Agent_ID: 3},
}

var Users = []structs.User{
{Username: "JohnDoe", Password: "password123", UserID: "12345", APIToken: []string{"token1", "token2", "token3"}},
{Username: "JaneDoe", Password: "password456", UserID: "67890", APIToken: []string{"token4", "token5", "token6"}},
}
Loading

0 comments on commit c0b1c9a

Please sign in to comment.