sshfp: added support for calling consul binary directly
Signed-off-by: Artur Marud <artur@status.im> Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
68db7fadb9
commit
1d070a80c4
|
@ -1,7 +1,7 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
//ConsulToken string `json:"consulKey"`
|
ConsulToken string `json:"consulToken"`
|
||||||
CloudflareToken string `json:"cloudflareKey"`
|
CloudflareToken string `json:"cloudflareKey"`
|
||||||
DomainName string `json:"domain"`
|
DomainName string `json:"domain"`
|
||||||
HostTimeout int `json:"hostTimeout"`
|
HostTimeout int `json:"hostTimeout"`
|
||||||
|
|
|
@ -23,6 +23,10 @@ func (s *service) LoadConfig(fileName string) (*Config, error) {
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, errors.New("cannot find env variable CF_TOKEN")
|
return nil, errors.New("cannot find env variable CF_TOKEN")
|
||||||
}
|
}
|
||||||
|
consulToken, exists := os.LookupEnv("CONSUL_TOKEN")
|
||||||
|
if !exists {
|
||||||
|
return nil, errors.New("cannot find env variable CF_TOKEN")
|
||||||
|
}
|
||||||
domainaName, exists := os.LookupEnv("DOMAIN_NAME")
|
domainaName, exists := os.LookupEnv("DOMAIN_NAME")
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, errors.New("cannot find env variable DOMAIN_NAME")
|
return nil, errors.New("cannot find env variable DOMAIN_NAME")
|
||||||
|
@ -48,7 +52,7 @@ func (s *service) LoadConfig(fileName string) (*Config, error) {
|
||||||
return nil, errors.New("cannot find env variable STORAGE_FILEPATH")
|
return nil, errors.New("cannot find env variable STORAGE_FILEPATH")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Config{CloudflareToken: cfToken, DomainName: domainaName, HostTimeout: int(hostTimeoutInt), LogLevel: logLevel, StorageFilePath: storageFilePath}, nil
|
return &Config{ConsulToken: consulToken, CloudflareToken: cfToken, DomainName: domainaName, HostTimeout: int(hostTimeoutInt), LogLevel: logLevel, StorageFilePath: storageFilePath}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,19 +3,19 @@ package consul
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type repo struct {
|
type repo struct {
|
||||||
rawData []byte
|
rawData []byte
|
||||||
|
reader io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
//Create new file repository satisfying Repository interface
|
//Create new file repository satisfying Repository interface
|
||||||
func NewStdinRepository() Repository {
|
func NewStdinRepository(reader io.Reader) Repository {
|
||||||
logrus.Debug("consul: Creating stdin reader")
|
logrus.Debug("consul: Creating stdin reader")
|
||||||
return &repo{}
|
return &repo{reader: reader}
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetData - Load data from stdin and store in the memory
|
//GetData - Load data from stdin and store in the memory
|
||||||
|
@ -23,7 +23,7 @@ func (r *repo) GetData() error {
|
||||||
var err error
|
var err error
|
||||||
logrus.Debug("consul: GetData: Opening stdin")
|
logrus.Debug("consul: GetData: Opening stdin")
|
||||||
|
|
||||||
rawData, err := io.ReadAll(os.Stdin)
|
rawData, err := io.ReadAll(r.reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
11
go.mod
11
go.mod
|
@ -3,13 +3,16 @@ module infra-sshfp-cf
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cloudflare/cloudflare-go v0.41.0 // indirect
|
github.com/cloudflare/cloudflare-go v0.41.0
|
||||||
|
github.com/sirupsen/logrus v1.8.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/google/go-cmp v0.5.7 // indirect
|
||||||
github.com/google/go-querystring v1.1.0 // indirect
|
github.com/google/go-querystring v1.1.0 // indirect
|
||||||
github.com/mattn/go-sqlite3 v1.14.13 // indirect
|
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
||||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
|
||||||
golang.org/x/text v0.3.7 // indirect
|
golang.org/x/text v0.3.7 // indirect
|
||||||
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
|
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
|
||||||
)
|
)
|
||||||
|
|
14
go.sum
14
go.sum
|
@ -1,26 +1,28 @@
|
||||||
github.com/cloudflare/cloudflare-go v0.40.0 h1:OjW+SYY7+NVSTj+/6kORqvu33LH7uZ0hUd/0qOqucxU=
|
|
||||||
github.com/cloudflare/cloudflare-go v0.40.0/go.mod h1:MmAqiRfD8rjKEuUe4MYNHfHjYhFWfW7PNe12CCQWqPY=
|
|
||||||
github.com/cloudflare/cloudflare-go v0.41.0 h1:4HiWuBpBj1fMiyWDfIlLGxnuPuEbkLi+SZWq9tgCFfc=
|
github.com/cloudflare/cloudflare-go v0.41.0 h1:4HiWuBpBj1fMiyWDfIlLGxnuPuEbkLi+SZWq9tgCFfc=
|
||||||
github.com/cloudflare/cloudflare-go v0.41.0/go.mod h1:o0jm+vdFrhwy7GOT3PB/71JQ6kElUQcifPc2Z9KTxeE=
|
github.com/cloudflare/cloudflare-go v0.41.0/go.mod h1:o0jm+vdFrhwy7GOT3PB/71JQ6kElUQcifPc2Z9KTxeE=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
||||||
|
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
||||||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
||||||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
||||||
github.com/mattn/go-sqlite3 v1.14.13 h1:1tj15ngiFfcZzii7yd82foL+ks+ouQcj8j/TPq3fk1I=
|
|
||||||
github.com/mattn/go-sqlite3 v1.14.13/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
|
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
|
||||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 h1:M73Iuj3xbbb9Uk1DYhzydthsj6oOd6l9bpuFcNoUvTs=
|
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 h1:M73Iuj3xbbb9Uk1DYhzydthsj6oOd6l9bpuFcNoUvTs=
|
||||||
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|
37
main.go
37
main.go
|
@ -1,20 +1,23 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"infra-sshfp-cf/cloudflare"
|
"infra-sshfp-cf/cloudflare"
|
||||||
"infra-sshfp-cf/config"
|
"infra-sshfp-cf/config"
|
||||||
"infra-sshfp-cf/consul"
|
"infra-sshfp-cf/consul"
|
||||||
"infra-sshfp-cf/sshfp"
|
"infra-sshfp-cf/sshfp"
|
||||||
"infra-sshfp-cf/statestore"
|
"infra-sshfp-cf/statestore"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//Debug loglevel
|
//Debug loglevel
|
||||||
logrus.SetLevel(logrus.DebugLevel)
|
logrus.SetLevel(logrus.InfoLevel)
|
||||||
|
|
||||||
//Create configuration components
|
//Create configuration components
|
||||||
// Get config file name from args, if empty - try to configure from ENVs
|
// Get config file name from args, if empty - try to configure from ENVs
|
||||||
var configFilename string = ""
|
var configFilename string = ""
|
||||||
|
@ -28,11 +31,37 @@ func main() {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch strings.ToLower(config.LogLevel) {
|
||||||
|
case "info":
|
||||||
|
logrus.SetLevel(logrus.InfoLevel)
|
||||||
|
case "debug":
|
||||||
|
logrus.SetLevel(logrus.DebugLevel)
|
||||||
|
default:
|
||||||
|
logrus.SetLevel(logrus.InfoLevel)
|
||||||
|
}
|
||||||
|
|
||||||
//Create cloudflare components
|
//Create cloudflare components
|
||||||
cloudflare := cloudflare.NewService(cloudflare.NewRepository(config.CloudflareToken, config.DomainName))
|
cloudflare := cloudflare.NewService(cloudflare.NewRepository(config.CloudflareToken, config.DomainName))
|
||||||
|
|
||||||
//Create STDIN Listener and start listening on (blocking operation)
|
//Create buffer to catch output from consul
|
||||||
consul := consul.NewService(consul.NewStdinRepository())
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
//Try run consul binary and catch output
|
||||||
|
logrus.Debug("Calling binary")
|
||||||
|
output, err := exec.Command("consul", "watch", "-type=service", "-service=sshd", "-token", config.ConsulToken, "-http-addr=http://127.0.0.1:8500").Output()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatalf("Cannot run consul, error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//Store output to the buffer
|
||||||
|
buf.Write(output)
|
||||||
|
|
||||||
|
//Use bufio Reader to turn bytes buffer into reader
|
||||||
|
bufferedReader := bufio.NewReader(&buf)
|
||||||
|
|
||||||
|
//Create reader listener and start reading on (blocking operation)
|
||||||
|
consul := consul.NewService(consul.NewStdinRepository(bufferedReader))
|
||||||
err = consul.LoadData()
|
err = consul.LoadData()
|
||||||
|
|
||||||
//Code below is executed upon data receipt
|
//Code below is executed upon data receipt
|
||||||
|
|
Loading…
Reference in New Issue