mirror of
https://github.com/nabbar/golib.git
synced 2025-10-05 07:46:56 +08:00

- Add DNS Mapper to force destination for a fqdn source - Allow wildcard and multi wildcard for fqdn source - DNS Mapper create Transport, Dialer & http client - DNS Mapper allow a config input to customize timeout, limit and TLS config - DNS Mapper use a gloabl transport connection poller - DNS Mapper implement DialContext & Dial method for transport - DNS Mapper use cache to accelerate process - DNS Mapper cache is only dnsmapper, not DNS cache - Replace old helper for http client with DNSMapper - Add default DNSMapper into the main lib of http client - Allow to overide the default DNS Mapper with a new one - Add ticker to force clean idle connection every given duration - "Compatible" with old config, as the default config will be used instead of old client build - Clean code and fix minor bugs - Add config component to allow use a global config - Config Component httpcli can overide default httpcli dns mapper when updated Package Certificates - Add function type to impose function that return a slice of rootCA string - update config/component/tls by replacing mutex with atomic - optimize some code Package Config - expand errors code index to add component httpcli Package Config/component: - database: add new validation, that config key existing into viper - head: add new validation, that config key existing into viper - http: add new validation, that config key existing into viper - ldap: add new validation, that config key existing into viper - log: add new validation, that config key existing into viper - mail: add new validation, that config key existing into viper - smtp: add new validation, that config key existing into viper Package AWS: - update package following update of httpcli - use interface for http cli that implement the Do function - update following the config/component/aws - rework config/component/aws to use atomic instead of mutex - update test unit following change Package Request: - update following http client update - use interface of HTTP Client that implement DO function, instead of http client struct pointer - update config & code following - apply same modification into config/component/request - update config/component/request by replacing mutex to atomic Package Server - add function Uptime to model to expose the duration since last start Package Semaphore - apply change name of constant following bump of lib associated Package Crypt - fix bug into reader to remove suffix of EOF if prevent Package Errors: - expand index module to add DNS Mapper client Package HTTP Server: - update monitor to apply changes Package Socket: - add additional check if reading error: if buffer has data, send it to handler before break run Other: - bump dependencies
258 lines
5.5 KiB
Go
258 lines
5.5 KiB
Go
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2020 Nicolas JUHEL
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*/
|
|
|
|
package aws_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/rand"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
lbuuid "github.com/hashicorp/go-uuid"
|
|
libaws "github.com/nabbar/golib/aws"
|
|
awscfg "github.com/nabbar/golib/aws/configCustom"
|
|
libhtc "github.com/nabbar/golib/httpcli"
|
|
libpwd "github.com/nabbar/golib/password"
|
|
libsiz "github.com/nabbar/golib/size"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var (
|
|
cli libaws.AWS
|
|
cfg libaws.Config
|
|
ctx context.Context
|
|
cnl context.CancelFunc
|
|
filename = "./config.json"
|
|
minioMode = false
|
|
)
|
|
|
|
/*
|
|
Using https://onsi.github.io/ginkgo/
|
|
Running with $> ginkgo -cover .
|
|
*/
|
|
|
|
func TestGolibAwsHelper(t *testing.T) {
|
|
RegisterFailHandler(Fail)
|
|
RunSpecs(t, "Aws Helper Suite")
|
|
}
|
|
|
|
var _ = BeforeSuite(func() {
|
|
var (
|
|
err error
|
|
name string
|
|
htp *http.Client
|
|
)
|
|
|
|
ctx, cnl = context.WithCancel(context.Background())
|
|
|
|
if err = loadConfig(); err != nil {
|
|
var (
|
|
uri = &url.URL{
|
|
Scheme: "http",
|
|
Host: "localhost:" + strconv.Itoa(GetFreePort()),
|
|
}
|
|
|
|
accessKey = libpwd.Generate(20)
|
|
secretKey = libpwd.Generate(64)
|
|
)
|
|
|
|
htp = libhtc.GetClient()
|
|
Expect(htp).NotTo(BeNil())
|
|
|
|
cfg = awscfg.NewConfig("", accessKey, secretKey, uri, "us-east-1")
|
|
Expect(cfg).NotTo(BeNil())
|
|
|
|
cfg.SetRegion("us-east-1")
|
|
err = cfg.RegisterRegionAws(nil)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
minioMode = true
|
|
|
|
go LaunchMinio(uri.Host, accessKey, secretKey)
|
|
|
|
for WaitMinio(uri.Host) {
|
|
time.Sleep(10 * time.Second)
|
|
}
|
|
|
|
time.Sleep(10 * time.Second)
|
|
println("Minio is waiting on : " + uri.Host)
|
|
}
|
|
|
|
cli, err = libaws.New(ctx, cfg, htp)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(cli).NotTo(BeNil())
|
|
|
|
err = cli.ForcePathStyle(ctx, true)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
name, err = lbuuid.GenerateUUID()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(name).ToNot(BeEmpty())
|
|
cli.Config().SetBucketName(name)
|
|
})
|
|
|
|
var _ = AfterSuite(func() {
|
|
cnl()
|
|
})
|
|
|
|
func loadConfig() error {
|
|
var (
|
|
cnfByt []byte
|
|
err error
|
|
)
|
|
|
|
if _, err = os.Stat(filename); err != nil {
|
|
return err
|
|
}
|
|
|
|
if cnfByt, err = ioutil.ReadFile(filename); err != nil {
|
|
return err
|
|
}
|
|
|
|
if cfg, err = awscfg.NewConfigJsonUnmashal(cnfByt); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = cfg.Validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func BuildPolicy() string {
|
|
return `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:Get*"],"Resource":["arn:aws:s3:::*/*"]}]}`
|
|
}
|
|
|
|
func BuildRole() string {
|
|
return `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"sts:AssumeRole","Principal":{"Service":"replication"}}]}`
|
|
}
|
|
|
|
func GetFreePort() int {
|
|
var (
|
|
addr *net.TCPAddr
|
|
lstn *net.TCPListener
|
|
err error
|
|
)
|
|
|
|
if addr, err = net.ResolveTCPAddr("tcp", "localhost:0"); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if lstn, err = net.ListenTCP("tcp", addr); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
defer func() {
|
|
_ = lstn.Close()
|
|
}()
|
|
|
|
return lstn.Addr().(*net.TCPAddr).Port
|
|
}
|
|
|
|
func GetTempFolder() string {
|
|
if tmp, err := ioutil.TempDir("", "minio-data-*"); err != nil {
|
|
panic(err)
|
|
} else {
|
|
if _, err = os.Stat(tmp); errors.Is(err, os.ErrNotExist) {
|
|
if err = os.Mkdir(tmp, 0700); err != nil {
|
|
panic(err)
|
|
}
|
|
} else if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return tmp
|
|
}
|
|
}
|
|
|
|
func DelTempFolder(folder string) {
|
|
if err := os.RemoveAll(folder); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func LaunchMinio(host, accessKey, secretKey string) {
|
|
os.Setenv("MINIO_ACCESS_KEY", accessKey)
|
|
os.Setenv("MINIO_SECRET_KEY", secretKey)
|
|
|
|
tmp := GetTempFolder()
|
|
defer DelTempFolder(tmp)
|
|
|
|
if _, minio, _, ok := runtime.Caller(0); ok {
|
|
if err := exec.CommandContext(ctx, filepath.Join(filepath.Dir(minio), "minio"), "server", "--address", host, tmp).Run(); err != nil {
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
panic(err)
|
|
}
|
|
} else {
|
|
//nolint #goerr113
|
|
panic(fmt.Errorf("minio execution file not found"))
|
|
}
|
|
|
|
//minio.Main([]string{"minio", "server", "--address", host, tmp})
|
|
}
|
|
|
|
func WaitMinio(host string) bool {
|
|
conn, err := net.DialTimeout("tcp", host, 10*time.Second)
|
|
|
|
defer func() {
|
|
if conn != nil {
|
|
_ = conn.Close()
|
|
}
|
|
}()
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
return err == nil
|
|
}
|
|
|
|
func randContent(size libsiz.Size) *bytes.Buffer {
|
|
p := make([]byte, size.Int64())
|
|
|
|
_, err := rand.Read(p)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return bytes.NewBuffer(p)
|
|
}
|