Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add credential using grpc metadata #1355

Merged
merged 3 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func initDatabases(cfg *config.Config) (*database.ServiceDB, *database.LevelDBIn
func deployCoreServices(cfg *config.Config, sdk *enginesdk.SDK) error {
for _, serviceConfig := range cfg.SystemServices {
logrus.WithField("module", "main").Infof("Deploying service %q", serviceConfig.Definition.Sid)
srv, err := sdk.Service.Create(serviceConfig.Definition)
srv, err := sdk.Service.Create(serviceConfig.Definition, "", "")
if err != nil {
existsError, ok := err.(*servicesdk.AlreadyExistsError)
if ok {
Expand Down
2 changes: 1 addition & 1 deletion execution/execution.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 22 additions & 38 deletions protobuf/api/account.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions protobuf/api/account.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ message CreateAccountResponse {
}

// The request's data for the `Delete` API.
message DeleteAccountRequest {
string name = 1;
string password = 2;
}
message DeleteAccountRequest {}

// The response's data for the `Delete` API.
message DeleteAccountResponse {}
4 changes: 2 additions & 2 deletions sdk/service/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func NewDeprecated(c container.Container, serviceDB *database.ServiceDB) Service
}

// Create creates a new service from definition.
func (s *deprecated) Create(req *api.CreateServiceRequest) (*service.Service, error) {
func (s *deprecated) Create(req *api.CreateServiceRequest, accountName, accountPassword string) (*service.Service, error) {
return create(s.container, s.serviceDB, req)
}

// Delete deletes the service by hash.
func (s *deprecated) Delete(hash hash.Hash) error {
func (s *deprecated) Delete(hash hash.Hash, accountName, accountPassword string) error {
return s.serviceDB.Delete(hash)
}

Expand Down
6 changes: 2 additions & 4 deletions sdk/service/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ func NewSDK(cdc *codec.Codec, client *cosmos.Client) Service {
}

// Create creates a new service from definition.
func (s *SDK) Create(req *api.CreateServiceRequest) (*service.Service, error) {
func (s *SDK) Create(req *api.CreateServiceRequest, accountName, accountPassword string) (*service.Service, error) {
// TODO: pass account by parameters
accountName, accountPassword := "bob", "pass"
accNumber, accSeq := uint64(0), uint64(0)
msg := newMsgCreateService(s.cdc, req)
tx, err := s.client.BuildAndBroadcastMsg(msg, accountName, accountPassword, accNumber, accSeq)
Expand All @@ -37,9 +36,8 @@ func (s *SDK) Create(req *api.CreateServiceRequest) (*service.Service, error) {
}

// Delete deletes the service by hash.
func (s *SDK) Delete(hash hash.Hash) error {
func (s *SDK) Delete(hash hash.Hash, accountName, accountPassword string) error {
// TODO: pass account by parameters
accountName, accountPassword := "bob", "pass"
accNumber, accSeq := uint64(0), uint64(0)
msg := newMsgDeleteService(s.cdc, hash)
_, err := s.client.BuildAndBroadcastMsg(msg, accountName, accountPassword, accNumber, accSeq)
Expand Down
4 changes: 2 additions & 2 deletions sdk/service/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

// Service is the interface of this sdk
type Service interface {
Create(*api.CreateServiceRequest) (*service.Service, error)
Delete(hash hash.Hash) error
Create(req *api.CreateServiceRequest, accountName string, accountPassword string) (*service.Service, error)
Delete(hash hash.Hash, accountName string, accountPassword string) error
Get(hash hash.Hash) (*service.Service, error)
List() ([]*service.Service, error)
}
Expand Down
6 changes: 5 additions & 1 deletion server/grpc/api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func (s *AccountServer) Get(ctx context.Context, request *protobuf_api.GetAccoun

// Delete an account
func (s *AccountServer) Delete(ctx context.Context, request *protobuf_api.DeleteAccountRequest) (*protobuf_api.DeleteAccountResponse, error) {
if err := s.sdk.Account.Delete(request.Name, request.Password); err != nil {
credname, credpassword, err := GetCredentialFromContext(ctx)
if err != nil {
return nil, err
}
if err := s.sdk.Account.Delete(credname, credpassword); err != nil {
return nil, err
}
return &protobuf_api.DeleteAccountResponse{}, nil
Expand Down
29 changes: 29 additions & 0 deletions server/grpc/api/credential.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package api

import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

// GetCredentialFromContext retrives credential name and password from request metadata.
func GetCredentialFromContext(ctx context.Context) (string, string, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return "", "", status.Errorf(codes.InvalidArgument, "missing metadata")
}

credentialUsernameMd := md["credential_username"]
if len(credentialUsernameMd) == 0 {
return "", "", status.Errorf(codes.Unauthenticated, "missing credential_username from metadata")
}

credentialPassphraseMd := md["credential_passphrase"]
if len(credentialPassphraseMd) == 0 {
return "", "", status.Errorf(codes.Unauthenticated, "missing credential_passphrase from metadata")
}

return credentialUsernameMd[0], credentialPassphraseMd[0], nil
}
26 changes: 17 additions & 9 deletions server/grpc/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,41 @@ func NewServiceServer(sdk *sdk.SDK) *ServiceServer {

// Create creates a new service from definition.
func (s *ServiceServer) Create(ctx context.Context, req *protobuf_api.CreateServiceRequest) (*protobuf_api.CreateServiceResponse, error) {
srv, err := s.sdk.Service.Create(req)
credUsername, credPassphrase, err := GetCredentialFromContext(ctx)
if err != nil {
return nil, err
}

srv, err := s.sdk.Service.Create(req, credUsername, credPassphrase)
if err != nil {
return nil, err
}
return &protobuf_api.CreateServiceResponse{Hash: srv.Hash}, nil
}

// Delete deletes service by hash or sid.
func (s *ServiceServer) Delete(ctx context.Context, request *protobuf_api.DeleteServiceRequest) (*protobuf_api.DeleteServiceResponse, error) {
func (s *ServiceServer) Delete(ctx context.Context, req *protobuf_api.DeleteServiceRequest) (*protobuf_api.DeleteServiceResponse, error) {
credUsername, credPassphrase, err := GetCredentialFromContext(ctx)
if err != nil {
return nil, err
}

// first, check if service has any running instances.
instances, err := s.sdk.Instance.List(&instancesdk.Filter{ServiceHash: request.Hash})
instances, err := s.sdk.Instance.List(&instancesdk.Filter{ServiceHash: req.Hash})
if err != nil {
return nil, err
}

if len(instances) > 0 {
return nil, errors.New("service has running instances. in order to delete the service, stop its instances first")
}
return &protobuf_api.DeleteServiceResponse{}, s.sdk.Service.Delete(request.Hash)

return &protobuf_api.DeleteServiceResponse{}, s.sdk.Service.Delete(req.Hash, credUsername, credPassphrase)
}

// Get returns service from given hash.
func (s *ServiceServer) Get(ctx context.Context, req *protobuf_api.GetServiceRequest) (*service.Service, error) {
service, err := s.sdk.Service.Get(req.Hash)
if err != nil {
return nil, err
}
return service, nil
return s.sdk.Service.Get(req.Hash)
}

// List returns all services.
Expand Down