backend/identity: add implementation with static data
This commit is contained in:
@@ -0,0 +1 @@
|
||||
identity
|
||||
@@ -0,0 +1,21 @@
|
||||
# Backstage Identity Service
|
||||
|
||||
Mock implementation of Backstage Indentity service API.
|
||||
|
||||
## Usage
|
||||
|
||||
```sh
|
||||
$ go build .
|
||||
|
||||
$ ./identity
|
||||
```
|
||||
|
||||
Try it out from `proto/` folder:
|
||||
|
||||
```sh
|
||||
prototool grpc identity \
|
||||
--address 0.0.0.0:50051 \
|
||||
--method spotify.backstage.identity.v1.Identity/GetUser \
|
||||
--data '{"id":"patriko"}' \
|
||||
--details
|
||||
```
|
||||
@@ -5,6 +5,7 @@ go 1.13
|
||||
replace github.com/spotify/backstage/proto => ../proto
|
||||
|
||||
require (
|
||||
github.com/golang/mock v1.1.1
|
||||
github.com/spotify/backstage/proto v0.0.0-00010101000000-000000000000
|
||||
google.golang.org/grpc v1.27.0
|
||||
)
|
||||
|
||||
@@ -5,12 +5,14 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
|
||||
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"users": [
|
||||
{
|
||||
"id": "rugvip",
|
||||
"name": "Patrik",
|
||||
"groups": ["backstage"]
|
||||
}
|
||||
],
|
||||
"groups": [
|
||||
{
|
||||
"id": "spotify",
|
||||
"groups": ["backstage", "scio"]
|
||||
},
|
||||
{
|
||||
"id": "backstage",
|
||||
"groups": []
|
||||
},
|
||||
{
|
||||
"id": "scio",
|
||||
"groups": []
|
||||
}
|
||||
]
|
||||
}
|
||||
+11
-14
@@ -1,38 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/spotify/backstage/identity/model"
|
||||
"github.com/spotify/backstage/identity/service"
|
||||
|
||||
identityv1 "github.com/spotify/backstage/proto/identity/v1"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
const (
|
||||
port = ":50051"
|
||||
port = ":50051"
|
||||
dataPath = "./identity-data.json"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data, err := model.LoadIdentityData(dataPath)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to load identity data, %s", err)
|
||||
}
|
||||
|
||||
lis, err := net.Listen("tcp", port)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
|
||||
grpcServer := grpc.NewServer()
|
||||
identityv1.RegisterIdentityServer(grpcServer, &server{})
|
||||
identityv1.RegisterIdentityServer(grpcServer, service.New(data))
|
||||
|
||||
log.Println("Serving Identity Service")
|
||||
grpcServer.Serve(lis)
|
||||
}
|
||||
|
||||
type server struct {
|
||||
}
|
||||
|
||||
func (s *server) GetUser(ctx context.Context, req *identityv1.GetUserRequest) (*identityv1.GetUserReply, error) {
|
||||
return &identityv1.GetUserReply{}, nil
|
||||
}
|
||||
|
||||
func (s *server) GetGroup(ctx context.Context, req *identityv1.GetGroupRequest) (*identityv1.GetGroupReply, error) {
|
||||
return &identityv1.GetGroupReply{}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type IdentityFileData struct {
|
||||
Users []struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Groups []string `json:"groups"`
|
||||
}
|
||||
Groups []struct {
|
||||
ID string `json:"id"`
|
||||
Groups []string `json:"groups"`
|
||||
}
|
||||
}
|
||||
|
||||
// LoadIdentityData loads identity data from a json file.
|
||||
func LoadIdentityData(path string) (*IdentityData, error) {
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var identityFileData IdentityFileData
|
||||
if err := json.Unmarshal(data, &identityFileData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
identityData := IdentityData{
|
||||
Users: map[string]User{},
|
||||
Groups: map[string]Group{},
|
||||
}
|
||||
|
||||
for _, user := range identityFileData.Users {
|
||||
identityData.Users[user.ID] = User{
|
||||
ID: user.ID,
|
||||
Name: user.Name,
|
||||
Groups: user.Groups,
|
||||
}
|
||||
}
|
||||
|
||||
for _, group := range identityFileData.Groups {
|
||||
identityData.Groups[group.ID] = Group{
|
||||
ID: group.ID,
|
||||
Groups: group.Groups,
|
||||
}
|
||||
}
|
||||
|
||||
return &identityData, nil
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
type IdentityData struct {
|
||||
Users map[string]User
|
||||
Groups map[string]Group
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID string
|
||||
Name string
|
||||
Groups []string
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
ID string
|
||||
Users []string
|
||||
Groups []string
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/spotify/backstage/identity/model"
|
||||
identityv1 "github.com/spotify/backstage/proto/identity/v1"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type identityDataServer model.IdentityData
|
||||
|
||||
// New creates a new identity data server
|
||||
func New(data *model.IdentityData) identityv1.IdentityServer {
|
||||
return (*identityDataServer)(data)
|
||||
}
|
||||
|
||||
func (s *identityDataServer) GetUser(ctx context.Context, req *identityv1.GetUserRequest) (*identityv1.GetUserReply, error) {
|
||||
id := req.GetId()
|
||||
if id == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "ID is required in GetUserRequest")
|
||||
}
|
||||
|
||||
user, ok := s.Users[id]
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.NotFound, "User '%s' not found", id)
|
||||
}
|
||||
|
||||
var replyGroups []*identityv1.Group
|
||||
for _, groupID := range user.Groups {
|
||||
replyGroups = append(replyGroups, &identityv1.Group{
|
||||
Id: groupID,
|
||||
})
|
||||
}
|
||||
|
||||
return &identityv1.GetUserReply{
|
||||
User: &identityv1.User{Id: user.ID, Name: user.Name},
|
||||
Groups: replyGroups,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *identityDataServer) GetGroup(ctx context.Context, req *identityv1.GetGroupRequest) (*identityv1.GetGroupReply, error) {
|
||||
id := req.GetId()
|
||||
if id == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "ID is required in GetGroupRequest")
|
||||
}
|
||||
|
||||
group, ok := s.Groups[id]
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.NotFound, "Group '%s' not found", id)
|
||||
}
|
||||
|
||||
var replyUsers []*identityv1.User
|
||||
for _, userID := range group.Users {
|
||||
replyUsers = append(replyUsers, &identityv1.User{
|
||||
Id: userID,
|
||||
})
|
||||
}
|
||||
|
||||
var replyGroups []*identityv1.Group
|
||||
for _, groupID := range group.Groups {
|
||||
replyGroups = append(replyGroups, &identityv1.Group{
|
||||
Id: groupID,
|
||||
})
|
||||
}
|
||||
|
||||
return &identityv1.GetGroupReply{
|
||||
Group: &identityv1.Group{Id: group.ID, Users: replyUsers, Groups: replyGroups},
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user