Hello there! I am trying to authenticate to Ranche...
# general
b
Hello there! I am trying to authenticate to Rancher clusters via API, but I am having hard time doing so. What would be the idiomatic way to do that? So far, I have something like this:
Copy code
import (
	"context"

	clientbase "github.com/rancher/norman/clientbase"
	management "github.com/rancher/rancher/pkg/client/generated/management/v3"
	"k8s.io/client-go/rest"
)

type ClusterInfo struct {
	Name   string
	Config *rest.Config
}

func (c *Cloud) GetClusters(ctx context.Context) ([]cloud.ClusterInfo, error) {

	// AuthToken in form aaaa:ssss
	accessKey := strings.Split(c.AuthToken, ":")[0]
	secretKey := strings.Split(c.AuthToken, ":")[1]
	opts := &clientbase.ClientOpts{
		URL:       c.BaseUrl,
		AccessKey: accessKey,
		SecretKey: secretKey,
	}

	mc, err := management.NewClient(opts)
	if err != nil {
		log.Fatal(err)
	}

	clusters, err := mc.Cluster.List(nil)
	if err != nil {
		log.Fatal(err)
	}

	for _, cluster := range clusters.Data {
		slog.Info("found cluster:", "name", cluster.Name)

		caCert := cluster.CACert
		block, _ := pem.Decode([]byte(caCert))
		if block != nil && strings.Contains(caCert, "-----BEGIN CERTIFICATE-----") {
			caCert = string(block.Bytes)
		}

		c.Clusters[cluster.Name] = &cloud.ClusterInfo{
			Name: cluster.Name,
			Config: &rest.Config{
				Host:            cluster.Links["self"],
				BearerToken:     secretKey,
				TLSClientConfig: rest.TLSClientConfig{CAData: []byte(caCert)},
			},
		}
		fmt.Println("cluster config", "data", c.Clusters[cluster.Name])
	}

	var response []cloud.ClusterInfo
	for _, cluster := range c.Clusters {
		response = append(response, *cluster)
	}

	return response, nil
}
Should there be an authentication plugin equivalent to something like this for GCP? https://gist.github.com/ahmetb/548059cdbf12fb571e4e2f1e29c48997 I tried searching the web, docs, and querying GPT, but haven’t found out how to work with this properly