1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
package csbouncer
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
"github.com/crowdsecurity/crowdsec/pkg/models"
)
type LiveBouncer struct {
APIKey string `yaml:"api_key"`
APIUrl string `yaml:"api_url"`
InsecureSkipVerify *bool `yaml:"insecure_skip_verify"`
CertPath string `yaml:"cert_path"`
KeyPath string `yaml:"key_path"`
CAPath string `yaml:"ca_path"`
APIClient *apiclient.ApiClient
UserAgent string
}
// Config() fills the struct with configuration values from a file. It is not
// aware of .yaml.local files so it is recommended to use ConfigReader() instead
func (b *LiveBouncer) Config(configPath string) error {
reader, err := os.Open(configPath)
if err != nil {
return fmt.Errorf("unable to read config file '%s': %w", configPath, err)
}
return b.ConfigReader(reader)
}
func (b *LiveBouncer) ConfigReader(configReader io.Reader) error {
content, err := io.ReadAll(configReader)
if err != nil {
return fmt.Errorf("unable to read configuration: %w", err)
}
err = yaml.Unmarshal(content, b)
if err != nil {
return fmt.Errorf("unable to unmarshal configuration: %w", err)
}
if b.APIUrl == "" {
return fmt.Errorf("config does not contain LAPI url")
}
if !strings.HasSuffix(b.APIUrl, "/") {
b.APIUrl += "/"
}
if b.APIKey == "" && b.CertPath == "" && b.KeyPath == "" {
return fmt.Errorf("config does not contain LAPI key or certificate")
}
return nil
}
func (b *LiveBouncer) Init() error {
var (
err error
apiURL *url.URL
client *http.Client
caCertPool *x509.CertPool
InsecureSkipVerify bool
ok bool
)
apiURL, err = url.Parse(b.APIUrl)
if err != nil {
return fmt.Errorf("local API Url '%s': %w", b.APIUrl, err)
}
if b.CAPath != "" {
log.Infof("Using CA cert '%s'", b.CAPath)
caCert, err := ioutil.ReadFile(b.CAPath)
if err != nil {
return fmt.Errorf("unable to load CA certificate '%s': %w", b.CAPath, err)
}
caCertPool = x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
} else {
caCertPool = nil
}
if b.InsecureSkipVerify == nil {
InsecureSkipVerify = false
} else {
InsecureSkipVerify = *b.InsecureSkipVerify
}
if b.APIKey != "" {
var transport *apiclient.APIKeyTransport
log.Infof("Using API key auth")
if apiURL.Scheme == "https" {
transport = &apiclient.APIKeyTransport{
APIKey: b.APIKey,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
InsecureSkipVerify: InsecureSkipVerify,
},
},
}
} else {
transport = &apiclient.APIKeyTransport{
APIKey: b.APIKey,
}
}
client = transport.Client()
ok = true
}
if b.CertPath != "" && b.KeyPath != "" {
var certificate tls.Certificate
log.Infof("Using cert auth")
certificate, err = tls.LoadX509KeyPair(b.CertPath, b.KeyPath)
if err != nil {
return fmt.Errorf("unable to load certificate '%s' and key '%s': %w", b.CertPath, b.KeyPath, err)
}
client = &http.Client{}
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{certificate},
InsecureSkipVerify: InsecureSkipVerify,
},
}
ok = true
}
if !ok {
return errors.New("no API key nor certificate provided")
}
b.APIClient, err = apiclient.NewDefaultClient(apiURL, "v1", b.UserAgent, client)
if err != nil {
return fmt.Errorf("api client init: %w", err)
}
return nil
}
func (b *LiveBouncer) Get(value string) (*models.GetDecisionsResponse, error) {
filter := apiclient.DecisionsListOpts{
IPEquals: &value,
}
decision, resp, err := b.APIClient.Decisions.List(context.Background(), filter)
if err != nil {
if resp != nil && resp.Response != nil {
resp.Response.Body.Close()
}
return &models.GetDecisionsResponse{}, err
}
if resp != nil && resp.Response != nil {
resp.Response.Body.Close()
}
return decision, nil
}
|