-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metadata): authenticate /meta endpoints (#1250)
* feat(metadata): authenticate /meta endpoints * refactor(metadata): simplify marshal json * fix(test): update meta endpoints to require authenticated
- Loading branch information
Showing
12 changed files
with
661 additions
and
12 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
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,68 @@ | ||
package metadata | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"go.flipt.io/flipt/internal/config" | ||
"go.flipt.io/flipt/internal/info" | ||
"go.flipt.io/flipt/rpc/flipt/meta" | ||
"google.golang.org/genproto/googleapis/api/httpbody" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
) | ||
|
||
type Server struct { | ||
cfg *config.Config | ||
info info.Flipt | ||
|
||
meta.UnimplementedMetadataServiceServer | ||
} | ||
|
||
func NewServer(cfg *config.Config, info info.Flipt) *Server { | ||
return &Server{ | ||
cfg: cfg, | ||
info: info, | ||
} | ||
} | ||
|
||
// RegisterGRPC registers the server on the provided gRPC server instance. | ||
func (s *Server) RegisterGRPC(server *grpc.Server) { | ||
meta.RegisterMetadataServiceServer(server, s) | ||
} | ||
|
||
// GetConfiguration returns a HttpBody instance containing the Flipt instance's | ||
// configuration structure marshalled as JSON. | ||
func (s *Server) GetConfiguration(ctx context.Context, _ *emptypb.Empty) (*httpbody.HttpBody, error) { | ||
return response(ctx, s.cfg) | ||
} | ||
|
||
// GetInfo returns a HttpBody instance containing the Flipt instance's | ||
// runtime information marshalled as JSON. | ||
func (s *Server) GetInfo(ctx context.Context, _ *emptypb.Empty) (*httpbody.HttpBody, error) { | ||
return response(ctx, s.info) | ||
} | ||
|
||
func response(ctx context.Context, v any) (*httpbody.HttpBody, error) { | ||
data, err := marshal(ctx, v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &httpbody.HttpBody{ | ||
ContentType: "application/json", | ||
Data: data, | ||
}, nil | ||
} | ||
|
||
func marshal(ctx context.Context, v any) ([]byte, error) { | ||
if md, ok := metadata.FromIncomingContext(ctx); ok { | ||
accept := md.Get("grpcgateway-accept") | ||
if len(accept) > 0 && accept[0] == "application/json+pretty" { | ||
return json.MarshalIndent(v, "", " ") | ||
} | ||
} | ||
|
||
return json.Marshal(v) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Oops, something went wrong.