Working with packages and allowed IPs.
giredorectl now able to interact with giredored about: * Setting package data. There is no such thing as "create" or "update", just set. * Deleting package data. * Setting allowed IP addresses. This is the only authorization method ATM, more may come in future.
This commit is contained in:
38
domains/client/v1/config.go
Normal file
38
domains/client/v1/config.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package clientv1
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/requester"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
)
|
||||
|
||||
func GetConfiguration(options map[string]string) {
|
||||
url := "http://" + options["server"] + "/_api/configuration"
|
||||
log.Info().Msg("Getting configuration from giredore server...")
|
||||
|
||||
data, err := requester.Get(url)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to get configuration from giredore server!")
|
||||
}
|
||||
|
||||
log.Debug().Msg("Got data: " + string(data))
|
||||
}
|
||||
|
||||
func SetAllowedIPs(args []string, options map[string]string) {
|
||||
url := "http://" + options["server"] + "/_api/configuration/allowedips"
|
||||
log.Info().Str("allowed IPs", args[0]).Msg("Setting allowed IPs for API interaction...")
|
||||
|
||||
req := &structs.AllowedIPsSetRequest{
|
||||
AllowedIPs: strings.Split(args[0], ","),
|
||||
}
|
||||
|
||||
data, err := requester.Post(url, req)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to set allowed IPs in giredore server configuration!")
|
||||
}
|
||||
|
||||
log.Debug().Msg("Got data: " + string(data))
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
package clientv1
|
||||
|
||||
import (
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/requester"
|
||||
)
|
||||
|
||||
func GetConfiguration(options map[string]string) {
|
||||
url := "http://" + options["server"] + "/_api/configuration"
|
||||
log.Info().Msg("Getting configuration from giredore server...")
|
||||
|
||||
data, err := requester.Get(url)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to get configuration from giredore server!")
|
||||
}
|
||||
|
||||
log.Debug().Msg("Got data: " + string(data))
|
||||
}
|
66
domains/client/v1/packages.go
Normal file
66
domains/client/v1/packages.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package clientv1
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/requester"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
)
|
||||
|
||||
func DeletePackage(args []string, options map[string]string) {
|
||||
req := &structs.PackageDeleteRequest{
|
||||
OriginalPath: args[0],
|
||||
}
|
||||
|
||||
log.Info().Str("original path", req.OriginalPath).Msg("Sending package deletion request to giredored...")
|
||||
|
||||
url := "http://" + options["server"] + "/_api/packages"
|
||||
data, err := requester.Delete(url, req)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to send package deletion request to giredored")
|
||||
}
|
||||
|
||||
log.Debug().Msg("Got data: " + string(data))
|
||||
}
|
||||
|
||||
func GetPackages(args []string, options map[string]string) {
|
||||
pkgs := strings.Split(args[0], ",")
|
||||
|
||||
req := &structs.PackageGetRequest{}
|
||||
if pkgs[0] == "all" {
|
||||
req.All = true
|
||||
} else {
|
||||
req.PackageNames = pkgs
|
||||
}
|
||||
|
||||
url := "http://" + options["server"] + "/_api/packages"
|
||||
log.Info().Msg("Getting packages data from giredore server...")
|
||||
|
||||
data, err := requester.Post(url, req)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to get packages data from giredore server!")
|
||||
}
|
||||
|
||||
log.Debug().Msg("Got data: " + string(data))
|
||||
}
|
||||
|
||||
func SetPackage(args []string, options map[string]string) {
|
||||
pkg := &structs.Package{
|
||||
Description: args[0],
|
||||
OriginalPath: args[1],
|
||||
RealPath: args[2],
|
||||
VCS: args[3],
|
||||
}
|
||||
|
||||
log.Info().Str("description", pkg.Description).Str("original path", pkg.OriginalPath).Str("real path", pkg.RealPath).Str("VCS", pkg.VCS).Msg("Sending set/update request to giredored...")
|
||||
|
||||
url := "http://" + options["server"] + "/_api/packages"
|
||||
data, err := requester.Put(url, pkg)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to send package update/set request to giredored")
|
||||
}
|
||||
|
||||
log.Debug().Msg("Got data: " + string(data))
|
||||
}
|
@@ -4,6 +4,10 @@ import (
|
||||
// stdlib
|
||||
"net/http"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/configuration"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
|
||||
// other
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
@@ -12,3 +16,17 @@ import (
|
||||
func configurationGET(ec echo.Context) error {
|
||||
return ec.JSON(http.StatusOK, map[string]string{"result": "success"})
|
||||
}
|
||||
|
||||
func configurationAllowedIPsSET(ec echo.Context) error {
|
||||
req := &structs.AllowedIPsSetRequest{}
|
||||
if err := ec.Bind(req); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse allowed IPs set request")
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingAllowedIPsSetRequest}})
|
||||
}
|
||||
|
||||
log.Debug().Msgf("Got set allowed IPs request: %+v", req)
|
||||
|
||||
configuration.Cfg.SetAllowedIPs(req.AllowedIPs)
|
||||
|
||||
return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
||||
}
|
||||
|
@@ -17,5 +17,15 @@ func Initialize() {
|
||||
log = logger.Logger.With().Str("type", "domain").Str("package", "server").Int("version", 1).Logger()
|
||||
log.Info().Msg("Initializing...")
|
||||
|
||||
// Configuration-related.
|
||||
httpserver.Srv.GET("/_api/configuration", configurationGET)
|
||||
httpserver.Srv.POST("/_api/configuration/allowedips", configurationAllowedIPsSET)
|
||||
|
||||
// Packages-related.
|
||||
httpserver.Srv.POST("/_api/packages", packagesGET)
|
||||
httpserver.Srv.PUT("/_api/packages", packagesSET)
|
||||
httpserver.Srv.DELETE("/_api/packages", packagesDELETE)
|
||||
|
||||
// goimports serving.
|
||||
httpserver.Srv.GET("/*", throwGoImports)
|
||||
}
|
||||
|
13
domains/server/v1/goimports.go
Normal file
13
domains/server/v1/goimports.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package serverv1
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"net/http"
|
||||
|
||||
// other
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
func throwGoImports(ec echo.Context) error {
|
||||
return ec.String(http.StatusOK, "All OK here")
|
||||
}
|
72
domains/server/v1/packagesapi.go
Normal file
72
domains/server/v1/packagesapi.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package serverv1
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"net/http"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/configuration"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
|
||||
// other
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
// This function responsible for getting packages configuration.
|
||||
func packagesGET(ec echo.Context) error {
|
||||
req := &structs.PackageGetRequest{}
|
||||
if err := ec.Bind(req); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse package get request")
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingPackagesGetRequest}})
|
||||
}
|
||||
|
||||
log.Info().Msgf("Received package(s) info get request: %+v", req)
|
||||
|
||||
var pkgs map[string]*structs.Package
|
||||
var errors []structs.Error
|
||||
if req.All {
|
||||
pkgs = configuration.Cfg.GetAllPackagesInfo()
|
||||
} else {
|
||||
pkgs, errors = configuration.Cfg.GetPackagesInfo(req.PackageNames)
|
||||
}
|
||||
|
||||
if errors != nil && len(errors) > 0 {
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errors, Data: pkgs})
|
||||
}
|
||||
|
||||
return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess, Data: pkgs})
|
||||
}
|
||||
|
||||
// This function responsible for deleting package.
|
||||
func packagesDELETE(ec echo.Context) error {
|
||||
req := &structs.PackageDeleteRequest{}
|
||||
if err := ec.Bind(req); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse package delete request")
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingDeleteRequest}})
|
||||
}
|
||||
|
||||
log.Info().Msgf("Received package delete request: %+v", req)
|
||||
|
||||
errs := configuration.Cfg.DeletePackage(req)
|
||||
|
||||
if len(errs) > 0 {
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})
|
||||
}
|
||||
|
||||
return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
||||
}
|
||||
|
||||
// This function responsible for setting or updating packages.
|
||||
func packagesSET(ec echo.Context) error {
|
||||
req := &structs.Package{}
|
||||
if err := ec.Bind(req); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse package data")
|
||||
return ec.JSON(http.StatusBadRequest, nil)
|
||||
}
|
||||
|
||||
log.Info().Msgf("Received package set/update request: %+v", req)
|
||||
|
||||
configuration.Cfg.AddOrUpdatePackage(req)
|
||||
|
||||
return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
||||
}
|
Reference in New Issue
Block a user