【问题标题】:Type error returning an interface for grpc tls credentials类型错误返回 grpc tls 凭据的接口
【发布时间】:2021-08-09 19:47:29
【问题描述】:

我正在编写一些代码来为多个服务生成 grpc tls 凭据,因此我想以模块化的方式编写它,以便它可以通过各种方式接受证书。虽然我觉得代码已经变得意大利面条和混乱。

我的目标是让 GetCredentials()GetTls() 方法能够从不同的地方被调用和配置,以便为多个服务生成凭据。

我遇到的问题在于GetCfgCredentials()GetCfgTls(),我希望它们返回接口但我收到类型错误

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
}

【问题讨论】:

    标签: go ssl grpc


    【解决方案1】:

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

    需要实现TransportCredentials接口。

    【讨论】:

      猜你喜欢
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      相关资源
      最近更新 更多