cisco

Home Shitty cloud provider
Log | Files | Refs | Submodules | git clone https://git.ne02ptzero.me/git/cisco

commit 808ef09d926fa46d2fdbfee7acbd4808bab44733
parent 89b07202c84982fc48d6d240e752f101878cdb45
Author: Louis Solofrizzo <lsolofrizzo@online.net>
Date:   Fri,  8 Mar 2019 00:23:46 +0100

Config: Add basic configuration with an external YAML file

Also some works on the beginning of the ssh gateway

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>

Diffstat:
Mapi/CMakeLists.txt | 4++++
Aapi/cisco.conf | 7+++++++
Aapi/config.go | 35+++++++++++++++++++++++++++++++++++
Aapi/instance.go | 35+++++++++++++++++++++++++++++++++++
Mapi/main.go | 57+++++++++++++++------------------------------------------
Aapi/ssh.go | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Aapi/user.go | 15+++++++++++++++
Mgo.mod | 6++++--
8 files changed, 165 insertions(+), 44 deletions(-)

diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt @@ -1,3 +1,7 @@ add_go_component(lf-cloud-api main.go + user.go + instance.go + config.go + ssh.go ) diff --git a/api/cisco.conf b/api/cisco.conf @@ -0,0 +1,7 @@ +cert: /home/louis/Work/perso/api_cloud.crt +key: /home/louis/Work/perso/api_cloud.key +host: 127.0.0.1 +port: 8080 +ca: /home/louis/Work/perso/ca.crt +sshproxy: cloud.louifox.house +sshkey: /home/louis/Work/perso/cisco.key diff --git a/api/config.go b/api/config.go @@ -0,0 +1,35 @@ +package main + +import ( + "flag" + "io/ioutil" + + "gopkg.in/yaml.v2" +) + +type CiscoConfig struct { + Cert string `yaml:"cert" binding:"required"` + Key string `yaml:"key" binding:"required"` + Host string `yaml:"host" binding:"required"` + Port string `yaml:"port" binding:"required"` + CA string `yaml:"ca" binding:"required"` + SSHProxy string `yaml:"sshproxy" binding:"required"` + SSHKey string `yaml:"sshkey" binding:"required"` +} + +var Config CiscoConfig + +func setConfig() error { + filename := flag.String("config", "/etc/cisco.conf", "The yaml configuration file") + flag.Parse() + + source, err := ioutil.ReadFile(*filename) + if err != nil { + return err + } + err = yaml.Unmarshal(source, &Config) + if err != nil { + return err + } + return nil +} diff --git a/api/instance.go b/api/instance.go @@ -0,0 +1,35 @@ +package main + +import ( + "crypto/tls" + "github.com/kataras/iris" +) + +type Instance struct { + Name string `json:"name"` + Architecture string `json:"arch"` + OS string `json:"os"` +} + +func instance_list(ctx iris.Context) { +} + +func instance_add(ctx iris.Context) { + var instance Instance + + ctx.ReadJSON(&instance) + ctx.Writef("%s - %s - %s", instance.Name, instance.Architecture, instance.OS) +} + +func instance_exists(ctx iris.Context) { + var tls *tls.ConnectionState + + tls = ctx.Request().TLS + //log.Infof("%s", tls.PeerCertificates[0].Subject) + name := ctx.Params().Get("name") + ctx.Writef("Instance %s do exist, and you are %s", name, tls.PeerCertificates[0].Subject.CommonName) +} + +func instance_get(ctx iris.Context) { + +} diff --git a/api/main.go b/api/main.go @@ -11,45 +11,6 @@ import ( "github.com/kataras/iris" ) -type Instance struct { - Name string `json:"name"` - Architecture string `json:"arch"` - OS string `json:"os"` -} - -func user_keys(ctx iris.Context) { -} - -func user_init(ctx iris.Context) { -} - -func user_add_key(ctx iris.Context) { - -} - -func instance_list(ctx iris.Context) { -} - -func instance_add(ctx iris.Context) { - var instance Instance - - ctx.ReadJSON(&instance) - ctx.Writef("%s - %s - %s", instance.Name, instance.Architecture, instance.OS) -} - -func instance_exists(ctx iris.Context) { - var tls *tls.ConnectionState - - tls = ctx.Request().TLS - //log.Infof("%s", tls.PeerCertificates[0].Subject) - name := ctx.Params().Get("name") - ctx.Writef("Instance %s do exist, and you are %s", name, tls.PeerCertificates[0].Subject.CommonName) -} - -func instance_get(ctx iris.Context) { - -} - func main() { app := iris.New() app.Use(func(ctx iris.Context) { @@ -61,6 +22,11 @@ func main() { ctx.Next() }) + err := setConfig() + if err != nil { + panic(err) + } + app.Get("/user/keys", user_keys) app.Get("/user/init", user_init) app.Post("/user/add_key", user_add_key) @@ -70,13 +36,13 @@ func main() { app.Get("/instance/{name:string}", instance_get) /* TLS */ - cert, err := tls.LoadX509KeyPair("./api_cloud.crt", "./api_cloud.key") + cert, err := tls.LoadX509KeyPair(Config.Cert, Config.Key) if err != nil { log.Fatalf("Failed loading x509 key pair: %s", err) } certpool := x509.NewCertPool() - pem, err := ioutil.ReadFile("./ca.crt") + pem, err := ioutil.ReadFile(Config.CA) if err != nil { log.Fatalf("Failed to read client certificate authority: %v", err) } @@ -91,12 +57,19 @@ func main() { } tlsconfig.Rand = rand.Reader - listener, err := tls.Listen("tcp", "localhost:8080", &tlsconfig) + listener, err := tls.Listen("tcp", Config.Host + ":" + Config.Port, &tlsconfig) if err != nil { log.Fatalf("Failed to listen: %s", err) } + err = ssh_init() + if err != nil { + panic(err) + } + + ssh_user_list() + app.Run(iris.Listener(listener), iris.WithConfiguration(iris.Configuration{ FireMethodNotAllowed: true, })) diff --git a/api/ssh.go b/api/ssh.go @@ -0,0 +1,50 @@ +package main + +import ( + "io/ioutil" + + log "github.com/sirupsen/logrus" + "golang.org/x/crypto/ssh" +) + +var client *ssh.Client + +func ssh_user_list() error { + session, err := client.NewSession() + if err != nil { + return err + } + + defer session.Close() + + out, err := session.CombinedOutput("user ls") + if err != nil { + log.Fatal(err) + return err + } + log.Infof("%s", out) + return nil +} + +func ssh_init() error { + key, err := ioutil.ReadFile(Config.SSHKey) + if err != nil { + log.Fatalf("unable to read private key: %v", err) + } + + signer, err := ssh.ParsePrivateKey(key) + if err != nil { + log.Fatalf("unable to parse private key: %v", err) + } + + config := &ssh.ClientConfig{ + User: "admin", + Auth: []ssh.AuthMethod{ + ssh.PublicKeys(signer), + }, + HostKeyCallback: ssh.InsecureIgnoreHostKey(), + } + + client, err = ssh.Dial("tcp", Config.SSHProxy + ":22", config) + return err +} diff --git a/api/user.go b/api/user.go @@ -0,0 +1,15 @@ +package main + +import ( + "github.com/kataras/iris" +) + +func user_keys(ctx iris.Context) { +} + +func user_init(ctx iris.Context) { +} + +func user_add_key(ctx iris.Context) { + +} diff --git a/go.mod b/go.mod @@ -15,7 +15,7 @@ require ( github.com/iris-contrib/go.uuid v2.0.0+incompatible // indirect github.com/json-iterator/go v1.1.5 // indirect github.com/kataras/golog v0.0.0-20180321173939-03be10146386 // indirect - github.com/kataras/iris v11.1.1+incompatible // indirect + github.com/kataras/iris v11.1.1+incompatible github.com/kataras/pio v0.0.0-20190103105442-ea782b38602d // indirect github.com/klauspost/compress v1.4.1 // indirect github.com/klauspost/cpuid v1.2.0 // indirect @@ -24,5 +24,7 @@ require ( github.com/modern-go/reflect2 v1.0.1 // indirect github.com/ryanuber/columnize v2.1.0+incompatible // indirect github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect - github.com/sirupsen/logrus v1.3.0 // indirect + github.com/sirupsen/logrus v1.3.0 + golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 + gopkg.in/yaml.v2 v2.2.2 )