From 8b21341dc938fbd23aecfbfc398344288a936128 Mon Sep 17 00:00:00 2001 From: Vladimir Ermakov Date: Thu, 2 Nov 2023 15:29:21 +0100 Subject: [PATCH] remove ssh connector - only static config may be working --- .gitignore | 1 + .goreleaser.yml | 10 ++--- connector_ssh.go | 57 --------------------------- etc/sample_profile.yaml | 1 + provider.go | 87 +++++++++++++---------------------------- 5 files changed, 34 insertions(+), 122 deletions(-) delete mode 100644 connector_ssh.go diff --git a/.gitignore b/.gitignore index 3b735ec..2262520 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ # Go workspace file go.work +fleeting-plugin-openstack diff --git a/.goreleaser.yml b/.goreleaser.yml index 75de7f4..5ff2b53 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -5,11 +5,11 @@ builds: main: ./cmd/fleeting-plugin-openstack ldflags: >- -s -w - -X github.com/prometheus/common/version.Version={{.Version}} - -X github.com/prometheus/common/version.Revision={{.FullCommit}} - -X github.com/prometheus/common/version.Branch={{.Branch}} - -X github.com/prometheus/common/version.BuildUser=goreleaser@github-actions - -X github.com/prometheus/common/version.BuildDate={{time "20060102-15:04:05"}} + -X github.com/sardinasystems/fleeting-plugin-openstack.Version={{.Version}} + -X github.com/sardinasystems/fleeting-plugin-openstack.Revision={{.FullCommit}} + -X github.com/sardinasystems/fleeting-plugin-openstack.Branch={{.Branch}} + -X github.com/sardinasystems/fleeting-plugin-openstack.BuildUser=goreleaser@github-actions + -X github.com/sardinasystems/fleeting-plugin-openstack.BuildDate={{time "20060102-15:04:05"}} # Set the binary output location to bin/ so archive will comply with Sensu Go Asset structure binary: bin/{{ .ProjectName }} diff --git a/connector_ssh.go b/connector_ssh.go deleted file mode 100644 index c561f95..0000000 --- a/connector_ssh.go +++ /dev/null @@ -1,57 +0,0 @@ -package fpoc - -import ( - "context" - "crypto" - "fmt" - - "golang.org/x/crypto/ssh" - - "gitlab.com/gitlab-org/fleeting/fleeting/provider" -) - -type PrivPub interface { - crypto.PrivateKey - Public() crypto.PublicKey -} - -func (g *InstanceGroup) ssh(ctx context.Context, info *provider.ConnectInfo) error { - var key PrivPub - var err error - - if info.Key != nil { - priv, err := ssh.ParseRawPrivateKey(info.Key) - if err != nil { - return fmt.Errorf("reading private key: %w", err) - } - var ok bool - key, ok = priv.(PrivPub) - if !ok { - return fmt.Errorf("key doesn't export PublicKey()") - } - } else { - /* - key, err = rsa.GenerateKey(rand.Reader, 4096) - if err != nil { - return fmt.Errorf("generating private key: %w", err) - } - - info.Key = pem.EncodeToMemory( - &pem.Block{ - Type: "RSA PRIVATE KEY", - Bytes: x509.MarshalPKCS1PrivateKey(key.(*rsa.PrivateKey)), - }, - ) - */ - return fmt.Errorf("private key generation not supported") - } - - sshPubKey, err := ssh.NewPublicKey(key.Public()) - if err != nil { - return fmt.Errorf("generating ssh public key: %w", err) - } - - _ = sshPubKey - - return nil -} diff --git a/etc/sample_profile.yaml b/etc/sample_profile.yaml index ed496c0..f200530 100644 --- a/etc/sample_profile.yaml +++ b/etc/sample_profile.yaml @@ -9,6 +9,7 @@ properties: # key_name: oskey networks: - network: external + security_groups: allow_ipmi_ssh metadata: tags: gitlab-runner user_data: | diff --git a/provider.go b/provider.go index 9e3044a..0e87648 100644 --- a/provider.go +++ b/provider.go @@ -21,12 +21,10 @@ import ( var _ provider.InstanceGroup = (*InstanceGroup)(nil) type InstanceGroup struct { - Cloud string `json:"cloud"` // cloud to use - CloudsConfig string `json:"clouds_config"` // optional: path to clouds.yaml - Name string `json:"name"` // name of the group / cluster name - ClusterID string `json:"cluster_id"` // optional: cluster id - SSHPrivateKeyFile string `json:"ssh_file"` // required: ssh key path - SSHUser string `json:"ssh_user"` // required: ssh user to login + Cloud string `json:"cloud"` // cloud to use + CloudsConfig string `json:"clouds_config"` // optional: path to clouds.yaml + Name string `json:"name"` // name of the cluster + ClusterID string `json:"cluster_id"` // optional: cluster id size int clusteringClient *gophercloud.ServiceClient @@ -84,14 +82,12 @@ func (g *InstanceGroup) Init(ctx context.Context, log hclog.Logger, settings pro g.ClusterID = cluster.ID } - pemBytes, err := os.ReadFile(g.SSHPrivateKeyFile) - if err != nil { - return provider.ProviderInfo{}, fmt.Errorf("SSH Private key file required: %w", err) + if !settings.ConnectorConfig.UseStaticCredentials { + return provider.ProviderInfo{}, fmt.Errorf("Only static credentials supported") } g.settings = settings g.log = log.With("name", g.Name, "cloud", g.Cloud, "cluster_name", cluster.Name, "cluster_id", cluster.ID) - g.settings.Key = pemBytes g.size = 0 if _, err := g.getNodes(ctx, true); err != nil { @@ -112,12 +108,14 @@ func (g *InstanceGroup) Update(ctx context.Context, update func(instance string, return err } - servers_, err := g.getServers(ctx, nodes_) - if err != nil { - return err - } - + var reterr error for _, node := range nodes_ { + srv, err := g.getServer(ctx, node.PhysicalID) + if err != nil { + reterr = errors.Join(reterr, err) + continue + } + state := provider.StateCreating switch node.Status { @@ -125,13 +123,9 @@ func (g *InstanceGroup) Update(ctx context.Context, update func(instance string, state = provider.StateDeleting case "ACTIVE", "OPERATING": - state = provider.StateRunning - } - - srv, ok := servers_[node.PhysicalID] - if ok { - // TODO: srv.Status? - _ = srv + if srv != nil { + state = provider.StateRunning + } } update(node.ID, state) @@ -206,23 +200,13 @@ func (g *InstanceGroup) getNodes(ctx context.Context, initial bool) ([]nodes.Nod return nodes, nil } -func (g *InstanceGroup) getServers(ctx context.Context, nodelist []nodes.Node) (map[string]*servers.Server, error) { - // Ideally i'd call server list with metadata.cluster_id=id, but we can't. - // So have to query each server - var reterr error - srvs := make(map[string]*servers.Server, len(nodelist)) - - for _, n := range nodelist { - srv, err := servers.Get(g.computeClient, n.PhysicalID).Extract() - if err != nil { - reterr = errors.Join(reterr, err) - g.log.Error("Failed to get server", "server_id", n.PhysicalID, "error", err) - continue - } - srvs[srv.ID] = srv +func (g *InstanceGroup) getServer(ctx context.Context, id string) (*servers.Server, error) { + srv, err := servers.Get(g.computeClient, id).Extract() + if errors.Is(err, &gophercloud.ErrResourceNotFound{}) { + return nil, nil } - return srvs, reterr + return srv, err } func (g *InstanceGroup) ConnectInfo(ctx context.Context, instanceID string) (provider.ConnectInfo, error) { @@ -231,10 +215,13 @@ func (g *InstanceGroup) ConnectInfo(ctx context.Context, instanceID string) (pro return provider.ConnectInfo{}, fmt.Errorf("Failed to get node %s: %w", instanceID, err) } - srv, err := servers.Get(g.computeClient, node.PhysicalID).Extract() + srv, err := g.getServer(ctx, node.PhysicalID) if err != nil { return provider.ConnectInfo{}, fmt.Errorf("Failed to get server %s: %w", node.PhysicalID, err) } + if srv == nil { + return provider.ConnectInfo{}, fmt.Errorf("Server not found %s: %w", node.PhysicalID, os.ErrNotExist) + } if srv.Status != "ACTIVE" { return provider.ConnectInfo{}, fmt.Errorf("instance status is not active: %s", srv.Status) @@ -242,6 +229,8 @@ func (g *InstanceGroup) ConnectInfo(ctx context.Context, instanceID string) (pro // TODO: get image metadata and get os_admin_user + g.log.Info("srv", "srv", srv) + info := provider.ConnectInfo{ ConnectorConfig: g.settings.ConnectorConfig, InternalAddr: srv.AccessIPv4, @@ -251,28 +240,6 @@ func (g *InstanceGroup) ConnectInfo(ctx context.Context, instanceID string) (pro // TODO: get from image meta info.OS = "linux" info.Arch = "amd64" - if info.Username == "" { - info.Username = g.SSHUser - } - - if info.UseStaticCredentials { - return info, nil - } - - if info.Protocol == "" { - info.Protocol = provider.ProtocolSSH - } - - switch info.Protocol { - case provider.ProtocolSSH: - err = g.ssh(ctx, &info) - - case provider.ProtocolWinRM: - err = fmt.Errorf("winrm not supported") - } - if err != nil { - return provider.ConnectInfo{}, err - } return info, nil }