-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for certificates removal
- Loading branch information
1 parent
624ff6d
commit c6fb40c
Showing
8 changed files
with
140 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package server | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/rs/zerolog/log" | ||
"gitlab.rete.farm/sistemi/inca/provider" | ||
) | ||
|
||
func (inca *Inca) handlerCA(c *fiber.Ctx) error { | ||
p := provider.Get(c.Params("provider"), inca.Cfg.Providers) | ||
if p == nil { | ||
return c.SendStatus(fiber.StatusNotFound) | ||
} | ||
|
||
caCrt, err := (*p).CA() | ||
if err != nil { | ||
log.Error().Err(err).Msg("unable to retrieve CA certificate") | ||
return c.SendStatus(fiber.StatusBadRequest) | ||
} | ||
|
||
return c.SendStream(bytes.NewReader(caCrt.Bytes), len(caCrt.Bytes)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package server | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/rs/zerolog/log" | ||
"gitlab.rete.farm/sistemi/inca/provider" | ||
"gitlab.rete.farm/sistemi/inca/util" | ||
) | ||
|
||
func (inca *Inca) handlerCRT(c *fiber.Ctx) error { | ||
var ( | ||
name = c.Params("name") | ||
crtFname = crtFilename(name) | ||
keyFname = keyFilename(name) | ||
queryStrings = util.ParseQueryString(c.Request().URI().QueryString()) | ||
) | ||
if len(name) <= 3 { | ||
log.Error().Str("name", name).Msg("name too short") | ||
return c.SendStatus(fiber.StatusBadRequest) | ||
} | ||
|
||
data, err := (*inca.Cfg.Storage).Get(crtFname) | ||
if err == nil { | ||
log.Info().Str("fname", crtFname).Err(err).Msg("returning cached certificate") | ||
return c.SendStream(bytes.NewReader(data), len(data)) | ||
} | ||
|
||
p := provider.GetFor(name, queryStrings, (*inca.Cfg).Providers) | ||
if p == nil { | ||
log.Error().Str("name", name).Msg("no provider found") | ||
return c.SendStatus(fiber.StatusBadRequest) | ||
} | ||
|
||
crt, key, err := (*p).Get(name, queryStrings) | ||
if err != nil { | ||
log.Error().Err(err).Msg("unable to generate") | ||
return c.SendStatus(fiber.StatusBadRequest) | ||
} | ||
|
||
if err := (*inca.Cfg.Storage).Put(crtFname, crt); err != nil { | ||
log.Error().Err(err).Msg("unable to persist certificate") | ||
return c.SendStatus(fiber.StatusBadRequest) | ||
} | ||
|
||
if err := (*inca.Cfg.Storage).Put(keyFname, key); err != nil { | ||
log.Error().Err(err).Msg("unable to persist certificate") | ||
return c.SendStatus(fiber.StatusBadRequest) | ||
} | ||
|
||
return c.SendStream(bytes.NewReader(crt.Bytes), len(crt.Bytes)) | ||
} | ||
|
||
func crtFilename(name string) string { | ||
return fmt.Sprintf("%s.pem", name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package server | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func (inca *Inca) handlerKey(c *fiber.Ctx) error { | ||
keyFname := keyFilename(c.Params("name")) | ||
data, err := (*inca.Cfg.Storage).Get(keyFname) | ||
if err == nil { | ||
return c.SendStream(bytes.NewReader(data), len(data)) | ||
} | ||
|
||
log.Info().Str("fname", keyFname).Err(err).Msg("cached key not found") | ||
return c.SendStatus(fiber.StatusNotFound) | ||
} | ||
|
||
func keyFilename(name string) string { | ||
return fmt.Sprintf("%s.key", name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func (inca *Inca) handlerRevoke(c *fiber.Ctx) error { | ||
for _, asset := range []string{ | ||
crtFilename(c.Params("name")), | ||
keyFilename(c.Params("name")), | ||
} { | ||
if err := (*inca.Cfg.Storage).Del(asset); err != nil { | ||
log.Error().Err(err).Msg("unable to remove") | ||
return c.SendStatus(fiber.StatusBadRequest) | ||
} | ||
} | ||
return c.SendStatus(fiber.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters