go - 类型错误返回 grpc tls 凭据的接口(interface)

标签 go ssl grpc

我正在编写一些为多个服务生成 grpc tls 凭据的代码,因此我想以模块化的方式编写它,以便它可以通过各种方式接受证书。虽然我觉得代码已经变得意大利面条和困惑。
我的目标是 GetCredentials()GetTls()能够从不同位置调用和配置的方法,以便为多个服务生成凭据。
我遇到的问题在于 GetCfgCredentials()GetCfgTls() ,我希望他们返回接口(interface),但我得到一个类型错误

Cannot use 'credentials' (type credentials.TransportCredentials) as the type CfgCredentials Type does not implement 'CfgCredentials' as some methods are missing: GetCredentials() *credentials.TransportCredentials
有经验的人可以告诉我他们对我构建下面列出的代码的方式和错误的看法吗?我对此很陌生,并希望获得一些关于如何以最模块化和最灵活的方式创建它的指示。
var globalCreds credentials.TransportCredentials

var globalTlsConfig *tls.Config

type CfgCredentialsProvider interface {
    GetCredentials() *credentials.TransportCredentials
}

type CfgTlsProvider interface {
    GetTlsConfig() *tls.Config
}

type CfgCredentials interface {
    CfgCredentialsProvider
}

type CfgTls interface {
    CfgTlsProvider
}

func GetGlobalCreds() (credentials.TransportCredentials, error) {
    if globalCreds == nil {
        creds := credentials.NewTLS(globalTlsConfig)
        globalCreds = creds
    }
    return globalCreds, nil
}

func GetGlobalTls(CertificatePath, PrivateEnhancedMailPath string) (*tls.Config, error) {
    serverCert, err := tls.LoadX509KeyPair(CertificatePath, PrivateEnhancedMailPath)
    if err != nil {
        return nil, err
    }

    if globalTlsConfig == nil {
        tlsConfig := &tls.Config{
            Certificates: []tls.Certificate{serverCert},
        }
        globalTlsConfig = tlsConfig
    }
    return globalTlsConfig, nil
}

func GetCfgCredentials() (CfgCredentials, bool) {
    credentials, err := GetGlobalCreds()
    if err != nil {
        return nil, false
    }
    return credentials, true
}

func GetCfgTls(CertificatePath, PrivateEnhancedMailPath string) (CfgTls, bool) {
    tls, err := GetGlobalTls(CertificatePath, PrivateEnhancedMailPath)
    if err != nil {
        return nil, false
    }
    return tls, true
}

最佳答案

看起来像您看到的错误:Cannot use 'credentials' (type credentials.TransportCredentials) as the type CfgCredentials Type does not implement 'CfgCredentials' as some methods are missing: GetCredentials() *credentials.TransportCredentials是因为你的类型没有实现接口(interface)。
它需要实现TransportCredentials界面。

关于go - 类型错误返回 grpc tls 凭据的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67622361/

相关文章:

c++ - 当指针作为对方法的引用传递时,如何在 C++ 中使用 void* 来保存 uint32_t 的值

go - 如何使用适用于 Windows 的 GitLab 管道进行构建

image - 去画图像蒙版

go - gRPC 到远程服务器的带宽较慢

Java TLS 套接字 : No trusted certificate found

security - 操作系统不信任 SSL 证书

c# - gRPC 服务器 .net 框架 - 如何配置使用 TLS 证书

go - 将 go 例程的多个结果聚合到一个数组中

与 Protocol Buffers 集成?

java - 无法使用 Vert.x 通过 TLS 连接到主机