-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from nitrictech/feature/secret-service
Base Secret Service Plugin
- Loading branch information
Showing
5 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
Submodule contracts
updated
from ae6424 to 795f11
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,90 @@ | ||
// Copyright 2021 Nitric Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package grpc | ||
|
||
import ( | ||
"context" | ||
|
||
pb "github.com/nitric-dev/membrane/interfaces/nitric/v1" | ||
"github.com/nitric-dev/membrane/pkg/plugins/secret" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
// GRPC Interface for registered Nitric Secret Plugins | ||
type SecretServer struct { | ||
pb.UnimplementedSecretServiceServer | ||
secretPlugin secret.SecretService | ||
} | ||
|
||
func (s *SecretServer) checkPluginRegistered() error { | ||
if s.secretPlugin == nil { | ||
return status.Errorf(codes.Unimplemented, "Secret plugin not registered") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *SecretServer) Put(ctx context.Context, req *pb.SecretPutRequest) (*pb.SecretPutResponse, error) { | ||
if err := s.checkPluginRegistered(); err == nil { | ||
if r, err := s.secretPlugin.Put(&secret.Secret{ | ||
Name: req.GetSecret().GetName(), | ||
}, req.GetValue()); err == nil { | ||
return &pb.SecretPutResponse{ | ||
SecretVersion: &pb.SecretVersion{ | ||
Secret: &pb.Secret{ | ||
Name: r.SecretVersion.Secret.Name, | ||
}, | ||
Version: r.SecretVersion.Version, | ||
}, | ||
}, nil | ||
} else { | ||
return nil, NewGrpcError("SecretService.Put", err) | ||
} | ||
} else { | ||
return nil, err | ||
} | ||
} | ||
|
||
func (s *SecretServer) Access(ctx context.Context, req *pb.SecretAccessRequest) (*pb.SecretAccessResponse, error) { | ||
if err := s.checkPluginRegistered(); err == nil { | ||
if s, err := s.secretPlugin.Access(&secret.SecretVersion{ | ||
Secret: &secret.Secret{ | ||
Name: req.GetSecretVersion().GetSecret().GetName(), | ||
}, | ||
Version: req.GetSecretVersion().GetVersion(), | ||
}); err == nil { | ||
return &pb.SecretAccessResponse{ | ||
SecretVersion: &pb.SecretVersion{ | ||
Secret: &pb.Secret{ | ||
Name: s.SecretVersion.Secret.Name, | ||
}, | ||
Version: s.SecretVersion.Version, | ||
}, | ||
Value: s.Value, | ||
}, nil | ||
} else { | ||
return nil, NewGrpcError("SecretService.Access", err) | ||
} | ||
} else { | ||
return nil, err | ||
} | ||
} | ||
|
||
func NewSecretServer(secretPlugin secret.SecretService) pb.SecretServiceServer { | ||
return &SecretServer{ | ||
secretPlugin: secretPlugin, | ||
} | ||
} |
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,38 @@ | ||
// Copyright 2021 Nitric Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package secret | ||
|
||
import "fmt" | ||
|
||
type SecretService interface { | ||
// Put - Creates a new version for a given secret | ||
Put(*Secret, []byte) (*SecretPutResponse, error) | ||
// Access - Retrieves the value for a given secret version | ||
Access(*SecretVersion) (*SecretAccessResponse, error) | ||
} | ||
|
||
type UnimplementedSecretPlugin struct { | ||
SecretService | ||
} | ||
|
||
var _ SecretService = (*UnimplementedSecretPlugin)(nil) | ||
|
||
func (*UnimplementedSecretPlugin) Put(secret *Secret, value []byte) (*SecretPutResponse, error) { | ||
return nil, fmt.Errorf("UNIMPLEMENTED") | ||
} | ||
|
||
func (*UnimplementedSecretPlugin) Access(version *SecretVersion) (*SecretAccessResponse, error) { | ||
return nil, fmt.Errorf("UNIMPLEMENTED") | ||
} |
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,40 @@ | ||
// Copyright 2021 Nitric Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package secret | ||
|
||
// Secret - Represents a container for secret versions | ||
type Secret struct { | ||
Name string | ||
} | ||
|
||
// SecretVersion - A version of a secret | ||
type SecretVersion struct { | ||
Secret *Secret | ||
|
||
// Version - the specific secret version this represents | ||
// Specifying "latest" will always retrieve the latest version of the secret | ||
Version string | ||
} | ||
|
||
// SecretAccessResponse - Return value for a secret access request | ||
type SecretAccessResponse struct { | ||
SecretVersion *SecretVersion | ||
Value []byte | ||
} | ||
|
||
// SecretPutResponse - Return value for a secret put request | ||
type SecretPutResponse struct { | ||
SecretVersion *SecretVersion | ||
} |