Skip to content

Commit

Permalink
Run go fmt on project (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsabath committed Jun 30, 2023
2 parents bfd233d + ee2163d commit a6f03bd
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 78 deletions.
5 changes: 2 additions & 3 deletions tornjak-backend/api/agent/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
grpc "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"google.golang.org/grpc/health/grpc_health_v1"
debugServer "github.com/spiffe/spire-api-sdk/proto/spire/api/server/debug/v1"
agent "github.com/spiffe/spire-api-sdk/proto/spire/api/server/agent/v1"
debugServer "github.com/spiffe/spire-api-sdk/proto/spire/api/server/debug/v1"
entry "github.com/spiffe/spire-api-sdk/proto/spire/api/server/entry/v1"
types "github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"google.golang.org/grpc/health/grpc_health_v1"

tornjakTypes "github.com/spiffe/tornjak/tornjak-backend/pkg/agent/types"
)
Expand Down Expand Up @@ -58,7 +58,6 @@ func (s *Server) DebugServer(inp DebugServerRequest) (*DebugServerResponse, erro
return (*DebugServerResponse)(resp), nil
}


type ListAgentsRequest agent.ListAgentsRequest
type ListAgentsResponse agent.ListAgentsResponse

Expand Down
19 changes: 9 additions & 10 deletions tornjak-backend/api/agent/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type Server struct {
TornjakConfig *TornjakConfig

// Plugins
Db agentdb.AgentDB
Auth auth.Auth
Db agentdb.AgentDB
Auth auth.Auth
}

// config type, as defined by SPIRE
Expand Down Expand Up @@ -113,7 +113,6 @@ func (s *Server) debugServer(w http.ResponseWriter, r *http.Request) {

}


func (s *Server) agentList(w http.ResponseWriter, r *http.Request) {
var input ListAgentsRequest
buf := new(strings.Builder)
Expand Down Expand Up @@ -543,7 +542,7 @@ func (s *Server) home(w http.ResponseWriter, r *http.Request) {
}
}

func (s *Server) GetRouter() (*mux.Router) {
func (s *Server) GetRouter() *mux.Router {
rtr := mux.NewRouter()

// Home
Expand Down Expand Up @@ -593,7 +592,7 @@ func (s *Server) HandleRequests() {
if err != nil {
log.Fatal("Cannot Configure: ", err)
}

numPorts := 0
errChannel := make(chan error, 3)
rtr := s.GetRouter()
Expand Down Expand Up @@ -668,7 +667,7 @@ func (s *Server) HandleRequests() {
errChannel <- err
return
}

err = server.ListenAndServeTLS(certPath, keyPath)
err = errors.Errorf("%s server: Error serving: %v", tlsType, err)
errChannel <- err
Expand Down Expand Up @@ -751,10 +750,10 @@ func (s *Server) HandleRequests() {

// as errors come in, read them, and block
for i := 0; i < numPorts; i++ {
err := <- errChannel
err := <-errChannel
log.Printf("%v", err)
}

}

func stringFromToken(keyToken token.Token) (string, error) {
Expand Down Expand Up @@ -869,7 +868,7 @@ func (s *Server) VerifyConfiguration() error {
}

serverConfig := s.TornjakConfig.Server
if (serverConfig.HttpConfig == nil && serverConfig.TlsConfig == nil && serverConfig.MtlsConfig == nil) {
if serverConfig.HttpConfig == nil && serverConfig.TlsConfig == nil && serverConfig.MtlsConfig == nil {
return errors.New("'config > server' must have at least one of HTTP, TLS, or mTLS sections defined")
}

Expand All @@ -896,7 +895,7 @@ func (s *Server) Configure() error {
/* Configure Server */
serverConfig := s.TornjakConfig.Server
s.SpireServerAddr = serverConfig.SPIRESocket // for convenience

/* Configure Plugins */
// configure defaults for optional plugins, reconfigured if given
// TODO maybe we should not have this step at all
Expand Down
31 changes: 15 additions & 16 deletions tornjak-backend/api/agent/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ type TornjakConfig struct {
/* Server configuration*/

type serverConfig struct {
SPIRESocket string `hcl:"spire_socket_path"`
HttpConfig *httpConfig `hcl:"http"`
TlsConfig *tlsConfig `hcl:"tls"`
MtlsConfig *mtlsConfig `hcl:"mtls"`
SPIRESocket string `hcl:"spire_socket_path"`
HttpConfig *httpConfig `hcl:"http"`
TlsConfig *tlsConfig `hcl:"tls"`
MtlsConfig *mtlsConfig `hcl:"mtls"`
}

type httpConfig struct {
Enabled bool `hcl:"enabled"`
ListenPort int `hcl:"port"`
Enabled bool `hcl:"enabled"`
ListenPort int `hcl:"port"`
}

type tlsConfig struct {
Enabled bool `hcl:"enabled"`
ListenPort int `hcl:"port"`
Cert string `hcl:"cert"`
Key string `hcl:"key"`
Enabled bool `hcl:"enabled"`
ListenPort int `hcl:"port"`
Cert string `hcl:"cert"`
Key string `hcl:"key"`
}

type mtlsConfig struct {
Enabled bool `hcl:"enabled"`
ListenPort int `hcl:"port"`
Cert string `hcl:"cert"`
Key string `hcl:"key"`
Ca string `hcl:"ca"`
Enabled bool `hcl:"enabled"`
ListenPort int `hcl:"port"`
Cert string `hcl:"cert"`
Key string `hcl:"key"`
Ca string `hcl:"ca"`
}

/* Plugin types */
Expand All @@ -59,4 +59,3 @@ type pluginAuthKeycloak struct {
JwksURL string
RedirectURL string
}

8 changes: 4 additions & 4 deletions tornjak-backend/cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"os"
"path/filepath"

"github.com/hashicorp/hcl"
"github.com/pkg/errors"
"github.com/spiffe/spire/cmd/spire-server/cli/run"
"github.com/spiffe/spire/pkg/common/catalog"
agentapi "github.com/spiffe/tornjak/tornjak-backend/api/agent"
"github.com/urfave/cli/v2"
"github.com/hashicorp/hcl"
)

type cliOptions struct {
Expand All @@ -34,15 +34,15 @@ func main() {
Destination: &opt.genericOptions.configFile,
Required: false,
},
&cli.StringFlag {
&cli.StringFlag{
Name: "tornjak-config",
Value: "",
Usage: "Config file path for tornjak server",
Destination: &opt.genericOptions.tornjakFile,
Required: true,
},
&cli.BoolFlag {
Name: "expandEnv",
&cli.BoolFlag{
Name: "expandEnv",
Value: false,
Usage: "Expansion of variables in config files",
Destination: &opt.genericOptions.expandEnv,
Expand Down
2 changes: 1 addition & 1 deletion tornjak-backend/pkg/agent/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (

type Auth interface {
// Verify takes request and returns nil if allowed, err otherwise
Verify(r *http.Request) (error)
Verify(r *http.Request) error
}
62 changes: 30 additions & 32 deletions tornjak-backend/pkg/agent/auth/keycloak.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
package auth

import (
"os"
"fmt"
"strings"
"net/http"
"os"
"strings"
"time"
//"encoding/json"

"github.com/pkg/errors"
"github.com/golang-jwt/jwt/v4"
"github.com/MicahParks/keyfunc"
"github.com/golang-jwt/jwt/v4"
"github.com/pkg/errors"
)

type KeycloakVerifier struct {
jwks *keyfunc.JWKS
redirect string
jwks *keyfunc.JWKS
redirect string
api_permissions map[string][]string
role_mappings map[string][]string
role_mappings map[string][]string
}

func getAuthLogic() (map[string][]string, map[string][]string) {
// api call matches to list of strings, representing disjunction of requirements
api_permissions := map[string][]string {
api_permissions := map[string][]string{
// no auth token needed
"/": []string{},

// viewer
"/api/healthcheck": []string{"admin", "viewer"},
"/api/debugserver": []string{"admin", "viewer"},
"/api/agent/list": []string{"admin", "viewer"},
"/api/entry/list": []string{"admin", "viewer"},
"/api/tornjak/serverinfo": []string{"admin", "viewer"},
"/api/healthcheck": []string{"admin", "viewer"},
"/api/debugserver": []string{"admin", "viewer"},
"/api/agent/list": []string{"admin", "viewer"},
"/api/entry/list": []string{"admin", "viewer"},
"/api/tornjak/serverinfo": []string{"admin", "viewer"},
"/api/tornjak/selectors/list": []string{"admin", "viewer"},
"/api/tornjak/agents/list": []string{"admin", "viewer"},
"/api/tornjak/clusters/list": []string{"admin", "viewer"},
"/api/tornjak/agents/list": []string{"admin", "viewer"},
"/api/tornjak/clusters/list": []string{"admin", "viewer"},
// admin
"/api/agent/ban": []string{"admin"},
"/api/agent/delete": []string{"admin"},
"/api/agent/createjointoken": []string{"admin"},
"/api/entry/create": []string{"admin"},
"/api/entry/delete": []string{"admin"},
"/api/agent/ban": []string{"admin"},
"/api/agent/delete": []string{"admin"},
"/api/agent/createjointoken": []string{"admin"},
"/api/entry/create": []string{"admin"},
"/api/entry/delete": []string{"admin"},
"/api/tornjak/selectors/register": []string{"admin"},
"/api/tornjak/clusters/create": []string{"admin"},
"/api/tornjak/clusters/edit": []string{"admin"},
"/api/tornjak/clusters/delete": []string{"admin"},
"/api/tornjak/clusters/create": []string{"admin"},
"/api/tornjak/clusters/edit": []string{"admin"},
"/api/tornjak/clusters/delete": []string{"admin"},
}
role_mappings := map[string][]string {
role_mappings := map[string][]string{
"tornjak-viewer-realm-role": []string{"viewer"},
"tornjak-admin-realm-role": []string{"admin"},
"tornjak-admin-realm-role": []string{"admin"},
}
return api_permissions, role_mappings
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func NewKeycloakVerifier(httpjwks bool, jwksURL string, redirectURL string) (*Ke
return nil, err
}
api_permissions, role_mappings := getAuthLogic()
return &KeycloakVerifier {
return &KeycloakVerifier{
jwks: jwks,
redirect: redirectURL,
api_permissions: api_permissions,
Expand Down Expand Up @@ -124,8 +124,6 @@ func (v *KeycloakVerifier) getPermissions(jwt_roles []string) map[string]bool {
return permissions
}



func (v *KeycloakVerifier) requestPermissible(r *http.Request, permissions map[string]bool) bool {
requires := v.api_permissions[r.URL.Path]
for _, req := range requires {
Expand All @@ -134,12 +132,12 @@ func (v *KeycloakVerifier) requestPermissible(r *http.Request, permissions map[s
}
}
return false

}

func (v *KeycloakVerifier) isGoodRequest(r *http.Request, claims *KeycloakClaim) bool {
roles := claims.RealmAccess.Roles

permissions := v.getPermissions(roles)
return v.requestPermissible(r, permissions)
}
Expand All @@ -160,7 +158,7 @@ func (v *KeycloakVerifier) Verify(r *http.Request) error {
if err != nil {
return err
}

// parse token
claims := &KeycloakClaim{}
jwt_token, err := jwt.ParseWithClaims(token, claims, v.jwks.Keyfunc)
Expand All @@ -177,6 +175,6 @@ func (v *KeycloakVerifier) Verify(r *http.Request) error {
if !v.isGoodRequest(r, claims) {
return errors.New("Unauthorized request")
}

return nil
}
11 changes: 5 additions & 6 deletions tornjak-backend/pkg/agent/auth/keycloak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package auth
import (
//"github.com/pkg/errors"
"bytes"
"testing"
"fmt"
"flag"
"encoding/json"
"flag"
"fmt"
"net/http"

"testing"
//"github.com/spiffe/tornjak/tornjak-backend/pkg/agent/types"
)

var jwksURL string

func init() {
flag.StringVar(&jwksURL, "jwksURL", "", "JWKS Url")
}
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestNewKeycloakVerifier(t *testing.T) {

func TestGetToken(t *testing.T) {
// sample request with token
request_body, err := json.Marshal(map[string]string {
request_body, err := json.Marshal(map[string]string{
"name": "nobody",
})
if err != nil {
Expand Down Expand Up @@ -93,4 +93,3 @@ func TestGetToken(t *testing.T) {
t.Fatalf("ERROR: could not obtain access token from request with bearer token: %s", token)
}
}

6 changes: 3 additions & 3 deletions tornjak-backend/pkg/agent/auth/no_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package auth

import "net/http"

type NullVerifier struct {}
type NullVerifier struct{}

func NewNullVerifier() (*NullVerifier) {
return &NullVerifier {}
func NewNullVerifier() *NullVerifier {
return &NullVerifier{}
}

func (v *NullVerifier) Verify(r *http.Request) error {
Expand Down
9 changes: 6 additions & 3 deletions tornjak-backend/pkg/agent/db/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ func TestSelectorDB(t *testing.T) {

// TestClusterCreate checks edge cases involving CreateClusterEntry
// Uses functions NewLocalSqliteDB, db.GetClusters, db.CreateClusterEntry,
// db.GetAgentClusterName, db.GetClusterAgents
//
// db.GetAgentClusterName, db.GetClusterAgents
func TestClusterCreate(t *testing.T) {
cleanup()
defer cleanup()
Expand Down Expand Up @@ -385,7 +386,8 @@ func TestClusterCreate(t *testing.T) {

// TestClusterEdit checks edge cases involving EditClusterEntry
// uses NewLocalSqliteDB, db.CreateClusterEntry, db.EditClusterEntry,
// db.GetAgentClusterName, db.GetClusterAgents
//
// db.GetAgentClusterName, db.GetClusterAgents
func TestClusterEdit(t *testing.T) {
defer cleanup()
expBackoff := backoff.NewExponentialBackOff()
Expand Down Expand Up @@ -611,7 +613,8 @@ func TestClusterEdit(t *testing.T) {

// TestClusterDelete checks edge cases on DeleteClusterEntry
// uses NewLocalSqliteDB, db.GetClusters, db.CreateClusterEntry, db.EditClusterEntry
// db.DeleteClusterEntry, db.GetAgentClusterName, db.GetClusterAgents
//
// db.DeleteClusterEntry, db.GetAgentClusterName, db.GetClusterAgents
func TestClusterDelete(t *testing.T) {
defer cleanup()
expBackoff := backoff.NewExponentialBackOff()
Expand Down

0 comments on commit a6f03bd

Please sign in to comment.