Skip to content

Commit

Permalink
feat(server): support JSON encoding as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
streambinder committed Jul 19, 2022
1 parent 6f41950 commit 02ad193
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
5 changes: 1 addition & 4 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -106,9 +105,7 @@ var cmdGen = &cobra.Command{

fmt.Print(out.String())
case "json":
crtJson := strings.ReplaceAll(string(crtBuffer[:len(crtBuffer)-1]), "\n", "\\n")
keyJson := strings.ReplaceAll(string(keyBuffer[:len(keyBuffer)-1]), "\n", "\\n")
fmt.Printf(`{"crt":"%s","key":"%s"}`, crtJson, keyJson)
fmt.Printf(`{"crt":"%s","key":"%s"}`, util.BytesToJSON(crtBuffer), util.BytesToJSON(keyBuffer))
default: // raw
fmt.Printf("%s%s", string(crtBuffer), string(keyBuffer))
}
Expand Down
6 changes: 6 additions & 0 deletions server/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package server

import (
"bytes"
"fmt"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
"gitlab.rete.farm/sistemi/inca/pki"
"gitlab.rete.farm/sistemi/inca/provider"
"gitlab.rete.farm/sistemi/inca/util"
)

func (inca *Inca) handlerCA(c *fiber.Ctx) error {
Expand All @@ -22,5 +25,8 @@ func (inca *Inca) handlerCA(c *fiber.Ctx) error {
}

caCrtBytes := pki.ExportBytes(caCrt)
if strings.EqualFold(c.Get("Accept", "text/plain"), "application/json") {
return c.SendString(fmt.Sprintf(`{"crt":"%s"}`, util.BytesToJSON(caCrtBytes)))
}
return c.SendStream(bytes.NewReader(caCrtBytes), len(caCrtBytes))
}
10 changes: 9 additions & 1 deletion server/crt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package server

import (
"bytes"
"fmt"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
Expand All @@ -22,7 +24,10 @@ func (inca *Inca) handlerCRT(c *fiber.Ctx) error {

data, _, err := (*inca.Cfg.Storage).Get(name)
if err == nil {
log.Info().Str("name", name).Err(err).Msg("returning cached certificate")
log.Info().Str("name", name).Msg("returning cached certificate")
if strings.EqualFold(c.Get("Accept", "text/plain"), "application/json") {
return c.SendString(fmt.Sprintf(`{"crt":"%s"}`, util.BytesToJSON(data)))
}
return c.SendStream(bytes.NewReader(data), len(data))
}

Expand All @@ -44,5 +49,8 @@ func (inca *Inca) handlerCRT(c *fiber.Ctx) error {
}

crtData := pki.ExportBytes(crt)
if strings.EqualFold(c.Get("Accept", "text/plain"), "application/json") {
return c.SendString(fmt.Sprintf(`{"crt":"%s"}`, util.BytesToJSON(crtData)))
}
return c.SendStream(bytes.NewReader(crtData), len(crtData))
}
6 changes: 6 additions & 0 deletions server/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package server

import (
"bytes"
"fmt"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
"gitlab.rete.farm/sistemi/inca/util"
)

func (inca *Inca) handlerKey(c *fiber.Ctx) error {
_, data, err := (*inca.Cfg.Storage).Get(c.Params("name"))
if err == nil {
if strings.EqualFold(c.Get("Accept", "text/plain"), "application/json") {
return c.SendString(fmt.Sprintf(`{"key":"%s"}`, util.BytesToJSON(data)))
}
return c.SendStream(bytes.NewReader(data), len(data))
}

Expand Down
7 changes: 7 additions & 0 deletions util/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package util

import "strings"

func BytesToJSON(data []byte) string {
return strings.ReplaceAll(strings.TrimSuffix(string(data), "\n"), "\n", "\\n")
}

0 comments on commit 02ad193

Please sign in to comment.