簡體   English   中英

類型錯誤返回 grpc tls 憑據的接口

[英]Type error returning an interface for grpc tls credentials

我正在編寫一些為多個服務生成 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
}

看起來您看到的錯誤是: Cannot use 'credentials' (type credentials.TransportCredentials) as the type CfgCredentials Type does not implement 'CfgCredentials' as some methods are missing: GetCredentials() *credentials.TransportCredentials是因為您的類型沒有實現接口。

它需要實現TransportCredentials接口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM