Skip to content

Commit

Permalink
Package Certificates:
Browse files Browse the repository at this point in the history
- fix bug with cert type marshall/unmarshall
- add old config to allow retro compatibility
- add new type function to retrieve a tls root ca cert instead of a slice of string to get root ca

Package HTTPCli:
- fix default DNS Mapper
- optimze global DNS Mapper
- fix non closing sub goroutine

Package HTTPCli/DNS-Mapper:
- change request function of Root CA with function of root ca cert instance
- add function to return a root ca cert from a function that return a slice of root ca string

Package Config/Components:
- httpcli: bump sub package of certificate, httpcli
- httpcli: adjust code following bump
- httpcli: change request function of Root CA with function of root ca cert instance
- httpcli: add function to return a root ca cert from a function that return a slice of root ca string
- tls: change request function of Root CA with function of root ca cert instance
- tls: add function to return a root ca cert from a function that return a slice of root ca string

Package IOUtils/mapCloser:
- fix bug with mapcloser not stopped
- optimize code & goroutine

Package Logger:
- rework mapCloser call
- optimize mapClaoser managment

Package Request:
- rework error managment
- using []byte instead of buffer to read response body
- add free capability
- optimize memory consumption

Package Socket / Server:
- add filtering error capability
- add params to specify a function called on each new connection and before using the connection
- the new function param allow to update the network incomming connection (like buffer, deadline...)
- rework some useless atomic to direct value to optimize code

Package Socket/Delim:
- rework to optimize memory & variable use
- remove capabilities of update the instance when running, prefert recreate new one if necessary

Other:
- bump dependencies
- minor bug / fix
  • Loading branch information
nabbar committed Jan 14, 2025
1 parent 22b3645 commit 61a73ba
Show file tree
Hide file tree
Showing 67 changed files with 1,218 additions and 525 deletions.
3 changes: 3 additions & 0 deletions certificates/ca/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ type Cert interface {
cbor.Unmarshaler
fmt.Stringer

Len() int
AppendPool(p *x509.CertPool)
AppendBytes(p []byte) error
AppendString(str string) error
}

func Parse(str string) (Cert, error) {
Expand Down
24 changes: 24 additions & 0 deletions certificates/ca/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ type mod struct {
c []*x509.Certificate
}

func (m *mod) Len() int {
return len(m.c)
}

func (m *mod) AppendBytes(p []byte) error {
c := &mod{
c: make([]*x509.Certificate, 0),
}

if e := c.unMarshall(p); e != nil {
return e
}

for _, i := range c.c {
m.c = append(m.c, i)
}

return nil
}

func (m *mod) AppendString(str string) error {
return m.AppendBytes([]byte(str))
}

func ViperDecoderHook() libmap.DecodeHookFuncType {
return func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
var (
Expand Down
28 changes: 8 additions & 20 deletions certificates/certificates_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ package certificates_test

import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"

Expand All @@ -44,32 +41,23 @@ import (

type EmptyStruct struct{}

var (
keyFile string
pubFile string
const (
keyFile = "test_ed25519.key"
pubFile = "test_ed25519.pub"
)

// TestGolibEncodingAESHelper tests the Golib AES Encoding Helper function.
func TestGolibArchiveHelper(t *testing.T) {
func TestGolibCertificatesHelper(t *testing.T) {
time.Sleep(500 * time.Millisecond) // Adding delay for better testing synchronization
RegisterFailHandler(Fail) // Registering fail handler for better test failure reporting
RunSpecs(t, "Certificates Helper Suite") // Running the test suite for Encoding AES Helper
}

var _ = BeforeSuite(func() {
keyFile = filepath.Join(os.Getenv("GOPATH"), "src", strings.Replace(reflect.TypeOf(EmptyStruct{}).PkgPath(), "_test", "", -1), "test_ed25519.key")
pubFile = filepath.Join(os.Getenv("GOPATH"), "src", strings.Replace(reflect.TypeOf(EmptyStruct{}).PkgPath(), "_test", "", -1), "test_ed25519.pub")
})

var _ = AfterSuite(func() {
if keyFile != "" {
if _, e := os.Stat(keyFile); e == nil {
Expect(os.Remove(keyFile)).ToNot(HaveOccurred())
}
if _, e := os.Stat(keyFile); e == nil {
Expect(os.Remove(keyFile)).ToNot(HaveOccurred())
}
if pubFile != "" {
if _, e := os.Stat(pubFile); e == nil {
Expect(os.Remove(pubFile)).ToNot(HaveOccurred())
}
if _, e := os.Stat(pubFile); e == nil {
Expect(os.Remove(pubFile)).ToNot(HaveOccurred())
}
})
107 changes: 96 additions & 11 deletions certificates/certs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package certs

import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/rsa"
Expand Down Expand Up @@ -58,8 +59,28 @@ func cleanPem(s string) string {
return strings.TrimSpace(s)
}

func cleanPemByte(s []byte) []byte {
s = bytes.TrimSpace(s)

// remove \n\r
s = bytes.Trim(s, "\n")
s = bytes.Trim(s, "\r")

// do again if \r\n
s = bytes.Trim(s, "\n")
s = bytes.Trim(s, "\r")

return bytes.TrimSpace(s)
}

type Config interface {
Cert() (*tls.Certificate, error)

IsChain() bool
IsPair() bool

IsFile() bool
GetCerts() []string
}

type ConfigPair struct {
Expand All @@ -68,34 +89,75 @@ type ConfigPair struct {
}

func (c *ConfigPair) Cert() (*tls.Certificate, error) {
c.Key = cleanPem(c.Key)
c.Pub = cleanPem(c.Pub)

if c == nil {
return nil, ErrInvalidPairCertificate
} else if len(c.Key) < 1 || len(c.Pub) < 1 {
}

var (
k = cleanPemByte([]byte(c.Key))
p = cleanPemByte([]byte(c.Pub))
)

if len(k) < 1 || len(p) < 1 {
return nil, ErrInvalidPairCertificate
}

if _, e := os.Stat(c.Key); e == nil {
if b, e := os.ReadFile(c.Key); e == nil {
c.Key = cleanPem(string(b))
if _, e := os.Stat(string(k)); e == nil {
if b, e := os.ReadFile(string(k)); e == nil {
k = cleanPemByte(b)
}
}

if _, e := os.Stat(c.Pub); e == nil {
if b, e := os.ReadFile(c.Pub); e == nil {
c.Pub = cleanPem(string(b))
if _, e := os.Stat(string(p)); e == nil {
if b, e := os.ReadFile(string(p)); e == nil {
p = cleanPemByte(b)
}
}

if crt, err := tls.X509KeyPair([]byte(c.Pub), []byte(c.Key)); err != nil {
if crt, err := tls.X509KeyPair(p, k); err != nil {
return nil, err
} else {
return &crt, nil
}
}

func (c *ConfigPair) IsChain() bool {
return false
}

func (c *ConfigPair) IsPair() bool {
return true
}

func (c *ConfigPair) IsFile() bool {
if c == nil {
return false
}

var (
k = cleanPemByte([]byte(c.Key))
p = cleanPemByte([]byte(c.Pub))
)

if len(k) < 1 || len(p) < 1 {
return false
}

if _, e := os.Stat(string(k)); e == nil {
return true
}

if _, e := os.Stat(string(p)); e == nil {
return true
}

return false
}

func (c *ConfigPair) GetCerts() []string {
return []string{c.Key, c.Pub}
}

type ConfigChain string

func (c *ConfigChain) Cert() (*tls.Certificate, error) {
Expand Down Expand Up @@ -163,3 +225,26 @@ func (c *ConfigChain) getPrivateKey(der []byte) (crypto.PrivateKey, error) {
}
return nil, ErrInvalidPrivateKey
}
func (c *ConfigChain) IsChain() bool {
return true
}

func (c *ConfigChain) IsPair() bool {
return false
}

func (c *ConfigChain) IsFile() bool {
if c == nil {
return false
}

if _, e := os.Stat(string(*c)); e == nil {
return true
}

return false
}

func (c *ConfigChain) GetCerts() []string {
return []string{string(*c)}
}
Loading

0 comments on commit 61a73ba

Please sign in to comment.