-
Notifications
You must be signed in to change notification settings - Fork 113
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 #3926 from kobergj/ServiceAccounts
Service Accounts
- Loading branch information
Showing
7 changed files
with
128 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Enhancement: Service Accounts | ||
|
||
Makes reva ready for service accounts by introducing an serviceaccounts auth manager | ||
|
||
https://github.com/cs3org/reva/pull/3926 |
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,90 @@ | ||
package serviceaccounts | ||
|
||
import ( | ||
"context" | ||
|
||
authpb "github.com/cs3org/go-cs3apis/cs3/auth/provider/v1beta1" | ||
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
|
||
"github.com/cs3org/reva/v2/pkg/auth" | ||
"github.com/cs3org/reva/v2/pkg/auth/manager/registry" | ||
"github.com/cs3org/reva/v2/pkg/auth/scope" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type conf struct { | ||
ServiceUsers []serviceuser `mapstructure:"service_accounts"` | ||
} | ||
|
||
type serviceuser struct { | ||
ID string `mapstructure:"id"` | ||
Secret string `mapstructure:"secret"` | ||
} | ||
|
||
type manager struct { | ||
authenticate func(userID, secret string) error | ||
} | ||
|
||
func init() { | ||
registry.Register("serviceaccounts", New) | ||
} | ||
|
||
// Configure parses the map conf | ||
func (m *manager) Configure(config map[string]interface{}) error { | ||
c := &conf{} | ||
if err := mapstructure.Decode(config, c); err != nil { | ||
return errors.Wrap(err, "error decoding conf") | ||
} | ||
// only inmem authenticator for now | ||
a := &inmemAuthenticator{make(map[string]string)} | ||
for _, s := range c.ServiceUsers { | ||
a.m[s.ID] = s.Secret | ||
} | ||
m.authenticate = a.Authenticate | ||
return nil | ||
} | ||
|
||
// New creates a new manager for the 'service' authentication | ||
func New(conf map[string]interface{}) (auth.Manager, error) { | ||
m := &manager{} | ||
err := m.Configure(conf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
// Authenticate authenticates the service account | ||
func (m *manager) Authenticate(ctx context.Context, userID string, secret string) (*userpb.User, map[string]*authpb.Scope, error) { | ||
if err := m.authenticate(userID, secret); err != nil { | ||
return nil, nil, err | ||
} | ||
scope, err := scope.AddOwnerScope(nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return &userpb.User{ | ||
// TODO: more details for service users? | ||
Id: &userpb.UserId{ | ||
OpaqueId: userID, | ||
Type: userpb.UserType_USER_TYPE_SERVICE, | ||
Idp: "none", | ||
}, | ||
}, scope, nil | ||
} | ||
|
||
type inmemAuthenticator struct { | ||
m map[string]string | ||
} | ||
|
||
func (a *inmemAuthenticator) Authenticate(userID string, secret string) error { | ||
if secret == "" || a.m[userID] == "" { | ||
return errors.New("unknown user") | ||
} | ||
if a.m[userID] == secret { | ||
return nil | ||
} | ||
return errors.New("secrets do not match") | ||
} |
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