如何从以下 golang 函数生成具有 TTL 8h 的令牌?

我是编程新手,不知道在我的客户端 golang 程序的源代码中使用令牌生成客户端 api 函数。寻求一些建议。太感谢了。


源码包:https ://pkg.go.dev/github.com/gravitational/teleport/api/client#Client.UpsertToken


函数源代码:


func (c *Client) UpsertToken(ctx context.Context, token types.ProvisionToken) error {

    tokenV2, ok := token.(*types.ProvisionTokenV2)

    if !ok {

        return trace.BadParameter("invalid type %T", token)

    }

    _, err := c.grpc.UpsertToken(ctx, tokenV2, c.callOpts...)

    return trail.FromGRPC(err)

}


My code:

package main


import (

    "context"

    "crypto/tls"

    "fmt"

    "log"

    "os"

    "strings"

    "time"


    "github.com/gravitational/teleport/api/client"

    "github.com/gravitational/teleport/api/client/proto"

    "google.golang.org/grpc"

)


// Client is a gRPC Client that connects to a Teleport Auth server either

// locally or over ssh through a Teleport web proxy or tunnel proxy.

//

// This client can be used to cover a variety of Teleport use cases,

// such as programmatically handling access requests, integrating

// with external tools, or dynamically configuring Teleport.


type Client struct {

    // c contains configuration values for the client.

    //c Config

    // tlsConfig is the *tls.Config for a successfully connected client.

    tlsConfig *tls.Config

    // dialer is the ContextDialer for a successfully connected client.

    //dialer ContextDialer

    // conn is a grpc connection to the auth server.

    conn *grpc.ClientConn

    // grpc is the gRPC client specification for the auth server.

    grpc proto.AuthServiceClient

    // closedFlag is set to indicate that the connnection is closed.

    // It's a pointer to allow the Client struct to be copied.

    closedFlag *int32

    // callOpts configure calls made by this client.

    callOpts []grpc.CallOption

}


/*

type ProvisionToken interface {

    Resource

    // SetMetadata sets resource metatada

    SetMetadata(meta Metadata)

    // GetRoles returns a list of teleport roles

    // that will be granted to the user of the token

    // in the crendentials

}



输出:语法错误而不是创建令牌


湖上湖
浏览 110回答 1
1回答

守候你守候我

看来您的代码有很多错误。而且,很明显您遇到了语法错误。我相信您会在控制台中获得实际发生这些语法错误的行号。请理解 Golang 的语法以及如何调用函数以及我应该向这些函数传递多少参数。在查看您的代码后,我想指出几个错误。//It shouldn't be like thisctx, err, token, err2 := clt.UpsertToken(ctx, token)//Instead it should be like this err := clt.UpsertToken(ctx, token)//The return type of UpsertToken() method is error, you should use only one variable to receive this error.strings.Contains()函数有两个参数,但你传递了四个。请参阅此文档以获取string.Contains()您正在使用循环内的条件和从不分配t := 0和检查它。ifforincremented请参阅本文档以获取fmt.Printf()参考这个功能删除所有语法错误,然后只有您的代码将运行并交叉检查您的逻辑。如果您想查看语法错误示例,请在此处查看:https ://go.dev/play/p/Hhu48UqlPRF
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go