From f09690186ce2697f78626ba06e7ccfb586f1c7ee Mon Sep 17 00:00:00 2001 From: Travis Bischel Date: Wed, 7 Oct 2020 09:01:29 -0600 Subject: [PATCH] sasl structs: add shortcuts to for sasl.Mechanism This will make sasl easier to use at callsites. --- pkg/sasl/kerberos/kerberos.go | 11 +++++++++++ pkg/sasl/oauth/oauth.go | 11 +++++++++++ pkg/sasl/plain/plain.go | 11 +++++++++++ pkg/sasl/scram/scram.go | 22 ++++++++++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/pkg/sasl/kerberos/kerberos.go b/pkg/sasl/kerberos/kerberos.go index a1ad3093..6646d74a 100644 --- a/pkg/sasl/kerberos/kerberos.go +++ b/pkg/sasl/kerberos/kerberos.go @@ -38,6 +38,17 @@ type Auth struct { PersistAfterAuth bool } +// AsMechanism returns a sasl mechanism that will use a as credentials for all +// sasl sessions. +// +// This is a shortcut for using the Kerberos function and is useful when you do +// not need to live-rotate credentials. +func (a Auth) AsMechanism() sasl.Mechanism { + return Kerberos(func(context.Context) (Auth, error) { + return a, nil + }) +} + // Kerberos returns a sasl mechanism that will call authFn whenever sasl // authentication is needed. The returned Auth is used for a single session. func Kerberos(authFn func(context.Context) (Auth, error)) sasl.Mechanism { diff --git a/pkg/sasl/oauth/oauth.go b/pkg/sasl/oauth/oauth.go index 28c3364a..201c6283 100644 --- a/pkg/sasl/oauth/oauth.go +++ b/pkg/sasl/oauth/oauth.go @@ -27,6 +27,17 @@ type Auth struct { _internal struct{} // require explicit field initalization } +// AsMechanism returns a sasl mechanism that will use a as credentials for all +// sasl sessions. +// +// This is a shortcut for using the Oauth function and is useful when you do +// not need to live-rotate credentials. +func (a Auth) AsMechanism() sasl.Mechanism { + return Oauth(func(context.Context) (Auth, error) { + return a, nil + }) +} + // Oauth returns an OAUTHBEARER sasl mechanism that will call authFn whenever // authentication is needed. The returned Auth is used for a single session. func Oauth(authFn func(context.Context) (Auth, error)) sasl.Mechanism { diff --git a/pkg/sasl/plain/plain.go b/pkg/sasl/plain/plain.go index 38620819..1d2c1dd9 100644 --- a/pkg/sasl/plain/plain.go +++ b/pkg/sasl/plain/plain.go @@ -22,6 +22,17 @@ type Auth struct { _internal struct{} // require explicit field initalization } +// AsMechanism returns a sasl mechanism that will use a as credentials for all +// sasl sessions. +// +// This is a shortcut for using the Plain function and is useful when you do +// not need to live-rotate credentials. +func (a Auth) AsMechanism() sasl.Mechanism { + return Plain(func(context.Context) (Auth, error) { + return a, nil + }) +} + // Plain returns a sasl mechanism that will call authFn whenever sasl // authentication is needed. The returned Auth is used for a single session. func Plain(authFn func(context.Context) (Auth, error)) sasl.Mechanism { diff --git a/pkg/sasl/scram/scram.go b/pkg/sasl/scram/scram.go index 5b1cc619..25ebf0f9 100644 --- a/pkg/sasl/scram/scram.go +++ b/pkg/sasl/scram/scram.go @@ -55,6 +55,28 @@ type Auth struct { _internal struct{} // require explicit field initalization } +// AsSha256Mechanism returns a sasl mechanism that will use a as credentials for +// all sasl sessions. +// +// This is a shortcut for using the Sha256 function and is useful when you do +// not need to live-rotate credentials. +func (a Auth) AsSha256Mechanism() sasl.Mechanism { + return Sha256(func(context.Context) (Auth, error) { + return a, nil + }) +} + +// AsSha512Mechanism returns a sasl mechanism that will use a as credentials for +// all sasl sessions. +// +// This is a shortcut for using the Sha512 function and is useful when you do +// not need to live-rotate credentials. +func (a Auth) AsSha512Mechanism() sasl.Mechanism { + return Sha512(func(context.Context) (Auth, error) { + return a, nil + }) +} + // Sha256 returns a SCRAM-SHA-256 sasl mechanism that will call authFn // whenever authentication is needed. The returned Auth is used for a single // session.