-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add middleware to return the server version when requested, unless di…
…sabled
- Loading branch information
1 parent
41bd3d1
commit a042362
Showing
7 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package serverversion | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/authzed/authzed-go/pkg/requestmeta" | ||
"github.com/authzed/authzed-go/pkg/responsemeta" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors" | ||
"github.com/rs/zerolog/log" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
|
||
"github.com/authzed/spicedb/pkg/releases" | ||
) | ||
|
||
type handleServerVersion struct { | ||
isEnabled bool | ||
} | ||
|
||
func (r *handleServerVersion) ServerReporter(ctx context.Context, _ interceptors.CallMeta) (interceptors.Reporter, context.Context) { | ||
if r.isEnabled { | ||
if md, ok := metadata.FromIncomingContext(ctx); ok { | ||
if _, isRequestingVersion := md[string(requestmeta.RequestServerVersion)]; isRequestingVersion { | ||
version, err := releases.CurrentVersion() | ||
if err != nil { | ||
log.Ctx(ctx).Err(err).Msg("could not load current software version") | ||
return interceptors.NoopReporter{}, ctx | ||
} | ||
|
||
err = responsemeta.SetResponseHeaderMetadata(ctx, map[responsemeta.ResponseMetadataHeaderKey]string{ | ||
responsemeta.ServerVersion: version, | ||
}) | ||
if err != nil { | ||
log.Ctx(ctx).Err(err).Msg("could not report metadata") | ||
} | ||
} | ||
} | ||
} | ||
|
||
return interceptors.NoopReporter{}, ctx | ||
} | ||
|
||
// UnaryServerInterceptor returns a new interceptor which handles server version requests. | ||
func UnaryServerInterceptor(isEnabled bool) grpc.UnaryServerInterceptor { | ||
return interceptors.UnaryServerInterceptor(&handleServerVersion{isEnabled}) | ||
} | ||
|
||
// StreamServerInterceptor returns a new interceptor which handles server version requests. | ||
func StreamServerInterceptor(isEnabled bool) grpc.StreamServerInterceptor { | ||
return interceptors.StreamServerInterceptor(&handleServerVersion{isEnabled}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.