Skip to content

Commit

Permalink
Move logging library to shared package
Browse files Browse the repository at this point in the history
  • Loading branch information
santiagolizardo committed Oct 11, 2022
1 parent 65f15ba commit 1e81b52
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
tags
bom-go-mod.spdx
*.out
.idea
12 changes: 10 additions & 2 deletions agent/internal/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/reconmap/shared-lib/pkg/logging"
"net/http"
"os"

Expand All @@ -13,13 +14,19 @@ import (

var rsakeys map[string]*rsa.PublicKey

var logger = logging.GetLoggerInstance()

func GetPublicKeys() string {
rsakeys = make(map[string]*rsa.PublicKey)
var body map[string]string
keycloakHostname, _ := os.LookupEnv("RMAP_KEYCLOAK_HOSTNAME")
uri := keycloakHostname + "/realms/reconmap"
resp, _ := http.Get(uri)
json.NewDecoder(resp.Body).Decode(&body)
err := json.NewDecoder(resp.Body).Decode(&body)
if err != nil {
logger.Error(err)
}

return body["public_key"]
}

Expand All @@ -33,7 +40,8 @@ func CheckRequestToken(r *http.Request) error {
pubkey := "-----BEGIN PUBLIC KEY-----\n" + GetPublicKeys() + "\n-----END PUBLIC KEY-----"
key, err := jwt.ParseRSAPublicKeyFromPEM([]byte(pubkey))
if err != nil {
fmt.Errorf("validate: parse key: %w", err)
err := fmt.Errorf("validate: parse key: %w", err)
return err
}

token, err := jwt.Parse(tokenParam, func(jwtToken *jwt.Token) (interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/rmap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package main
import (
"encoding/base64"
"fmt"
"github.com/reconmap/shared-lib/pkg/logging"
"os"

"github.com/fatih/color"
"github.com/reconmap/cli/internal/build"
"github.com/reconmap/cli/internal/commands"
"github.com/reconmap/cli/internal/logging"
"github.com/urfave/cli/v2"
)

Expand Down
14 changes: 12 additions & 2 deletions cli/internal/commands/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/reconmap/shared-lib/pkg/logging"
"io/ioutil"
"log"
"net/http"
Expand All @@ -24,14 +25,18 @@ type IDTokenClaim struct {
}

func Login() error {
logger := logging.GetLoggerInstance()

config, err := configuration.ReadConfig()
if err != nil {
logger.Error(err)
return err
}

provider, err := oidc.NewProvider(oauth2.NoContext, config.AuthUrl+"/realms/reconmap")
if err != nil {
panic(err)
logger.Error(err)
return err
}

oauthConfig := oauth2.Config{
Expand All @@ -42,7 +47,12 @@ func Login() error {
}

var stateSeed uint64
binary.Read(rand.Reader, binary.LittleEndian, &stateSeed)
err = binary.Read(rand.Reader, binary.LittleEndian, &stateSeed)
if err != nil {
logger.Error(err)
return err
}

state := fmt.Sprintf("%x", stateSeed)

authCodeURL := oauthConfig.AuthCodeURL(state)
Expand Down
8 changes: 7 additions & 1 deletion cli/internal/commands/cmds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"github.com/reconmap/shared-lib/pkg/logging"
"log"
"os"
"os/exec"
Expand All @@ -14,6 +15,8 @@ import (
)

func RunCommand(command *api.Command, vars []string) error {
logger := logging.GetLoggerInstance()

var err error
if command.ExecutableType == "custom" {
argsRendered := terminal.ReplaceArgs(command, vars)
Expand Down Expand Up @@ -51,7 +54,10 @@ func RunCommand(command *api.Command, vars []string) error {
outputFilename := strconv.Itoa(command.ID) + ".out"
f, err := os.Create(outputFilename)
defer f.Close()
f.WriteString(outStr)
_, err = f.WriteString(outStr)
if err != nil {
logger.Error(err)
}
command.OutputFileName = outputFilename

if len(errStr) > 0 {
Expand Down
6 changes: 6 additions & 0 deletions shared-lib/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module github.com/reconmap/shared-lib

go 1.18

require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.23.0 // indirect
)
11 changes: 11 additions & 0 deletions shared-lib/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
File renamed without changes.

0 comments on commit 1e81b52

Please sign in to comment.