Multiple playgrounds support (#215)

* Add Playground struct and basic support for creating it and retrieving
it

* Add missing functions in pwd mock

* Get playground from request domain and validate it exists. If valid set
it on the newly created session.

* Move playground specific configurations to the playground struct and use
it everytime we need that conf.

* Don't allow to specify a duration bigger that the allowed in the
playground
This commit is contained in:
Jonathan Leibiusky
2017-11-14 15:50:04 -03:00
committed by GitHub
parent 3dee0d3f0b
commit 3f5b3882dd
24 changed files with 784 additions and 159 deletions

View File

@@ -22,6 +22,7 @@ type DB struct {
WindowsInstances map[string]*types.WindowsInstance `json:"windows_instances"`
LoginRequests map[string]*types.LoginRequest `json:"login_requests"`
Users map[string]*types.User `json:"user"`
Playgrounds map[string]*types.Playground `json:"playgrounds"`
WindowsInstancesBySessionId map[string][]string `json:"windows_instances_by_session_id"`
InstancesBySessionId map[string][]string `json:"instances_by_session_id"`
@@ -364,6 +365,25 @@ func (store *storage) UserGet(id string) (*types.User, error) {
}
}
func (store *storage) PlaygroundPut(playground *types.Playground) error {
store.rw.Lock()
defer store.rw.Unlock()
store.db.Playgrounds[playground.Id] = playground
return store.save()
}
func (store *storage) PlaygroundGet(id string) (*types.Playground, error) {
store.rw.Lock()
defer store.rw.Unlock()
if playground, found := store.db.Playgrounds[id]; !found {
return nil, NotFoundError
} else {
return playground, nil
}
return nil, NotFoundError
}
func (store *storage) load() error {
file, err := os.Open(store.path)
@@ -376,12 +396,13 @@ func (store *storage) load() error {
}
} else {
store.db = &DB{
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{},
@@ -392,6 +413,19 @@ func (store *storage) load() error {
file.Close()
return nil
}
func (store *storage) PlaygroundGetAll() ([]*types.Playground, error) {
store.rw.Lock()
defer store.rw.Unlock()
playgrounds := make([]*types.Playground, len(store.db.Playgrounds))
i := 0
for _, p := range store.db.Playgrounds {
playgrounds[i] = p
i++
}
return playgrounds, nil
}
func (store *storage) save() error {
file, err := os.Create(store.path)

View File

@@ -30,12 +30,13 @@ func TestSessionPut(t *testing.T) {
assert.Nil(t, err)
expectedDB := &DB{
Sessions: map[string]*types.Session{s.Id: s},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{s.Id: s},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{},
@@ -59,12 +60,13 @@ func TestSessionPut(t *testing.T) {
func TestSessionGet(t *testing.T) {
expectedSession := &types.Session{Id: "aaabbbccc"}
expectedDB := &DB{
Sessions: map[string]*types.Session{expectedSession.Id: expectedSession},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{expectedSession.Id: expectedSession},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{},
@@ -98,12 +100,13 @@ func TestSessionGetAll(t *testing.T) {
s1 := &types.Session{Id: "aaabbbccc"}
s2 := &types.Session{Id: "dddeeefff"}
expectedDB := &DB{
Sessions: map[string]*types.Session{s1.Id: s1, s2.Id: s2},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{s1.Id: s1, s2.Id: s2},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{},
@@ -163,12 +166,13 @@ func TestSessionDelete(t *testing.T) {
func TestInstanceGet(t *testing.T) {
expectedInstance := &types.Instance{SessionId: "aaabbbccc", Name: "i1", IP: "10.0.0.1"}
expectedDB := &DB{
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{expectedInstance.Name: expectedInstance},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{expectedInstance.Name: expectedInstance},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{expectedInstance.SessionId: []string{expectedInstance.Name}},
ClientsBySessionId: map[string][]string{},
@@ -217,12 +221,13 @@ func TestInstancePut(t *testing.T) {
assert.Nil(t, err)
expectedDB := &DB{
Sessions: map[string]*types.Session{s.Id: s},
Instances: map[string]*types.Instance{i.Name: i},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{s.Id: s},
Instances: map[string]*types.Instance{i.Name: i},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{i.SessionId: []string{i.Name}},
ClientsBySessionId: map[string][]string{},
@@ -280,12 +285,13 @@ func TestInstanceFindBySessionId(t *testing.T) {
i1 := &types.Instance{SessionId: "aaabbbccc", Name: "c1"}
i2 := &types.Instance{SessionId: "aaabbbccc", Name: "c2"}
expectedDB := &DB{
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{i1.Name: i1, i2.Name: i2},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{i1.Name: i1, i2.Name: i2},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{i1.SessionId: []string{i1.Name, i2.Name}},
ClientsBySessionId: map[string][]string{},
@@ -316,12 +322,13 @@ func TestWindowsInstanceGetAll(t *testing.T) {
i1 := &types.WindowsInstance{SessionId: "aaabbbccc", Id: "i1"}
i2 := &types.WindowsInstance{SessionId: "aaabbbccc", Id: "i2"}
expectedDB := &DB{
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{i1.Id: i1, i2.Id: i2},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{i1.Id: i1, i2.Id: i2},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{i1.SessionId: []string{i1.Id, i2.Id}},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{},
@@ -371,12 +378,13 @@ func TestWindowsInstancePut(t *testing.T) {
assert.Nil(t, err)
expectedDB := &DB{
Sessions: map[string]*types.Session{s.Id: s},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{i.Id: i},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{s.Id: s},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{i.Id: i},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{i.SessionId: []string{i.Id}},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{},
@@ -433,12 +441,13 @@ func TestWindowsInstanceDelete(t *testing.T) {
func TestClientGet(t *testing.T) {
c := &types.Client{SessionId: "aaabbbccc", Id: "c1"}
expectedDB := &DB{
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{c.Id: c},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{c.Id: c},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{c.SessionId: []string{c.Id}},
@@ -487,12 +496,13 @@ func TestClientPut(t *testing.T) {
assert.Nil(t, err)
expectedDB := &DB{
Sessions: map[string]*types.Session{s.Id: s},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{c.Id: c},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{s.Id: s},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{c.Id: c},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{c.SessionId: []string{c.Id}},
@@ -550,12 +560,13 @@ func TestClientFindBySessionId(t *testing.T) {
c1 := &types.Client{SessionId: "aaabbbccc", Id: "c1"}
c2 := &types.Client{SessionId: "aaabbbccc", Id: "c2"}
expectedDB := &DB{
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{c1.Id: c1, c2.Id: c2},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{c1.Id: c1, c2.Id: c2},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{c1.SessionId: []string{c1.Id, c2.Id}},
@@ -581,3 +592,120 @@ func TestClientFindBySessionId(t *testing.T) {
assert.Subset(t, clients, []*types.Client{c1, c2})
assert.Len(t, clients, 2)
}
func TestPlaygroundGet(t *testing.T) {
p := &types.Playground{Id: "aaabbbccc"}
expectedDB := &DB{
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{p.Id: p},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{},
UsersByProvider: map[string]string{},
}
tmpfile, err := ioutil.TempFile("", "pwd")
if err != nil {
log.Fatal(err)
}
encoder := json.NewEncoder(tmpfile)
err = encoder.Encode(&expectedDB)
assert.Nil(t, err)
tmpfile.Close()
defer os.Remove(tmpfile.Name())
storage, err := NewFileStorage(tmpfile.Name())
assert.Nil(t, err)
found, err := storage.PlaygroundGet("aaabbbccc")
assert.Nil(t, err)
assert.Equal(t, p, found)
}
func TestPlaygroundPut(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "pwd")
if err != nil {
log.Fatal(err)
}
tmpfile.Close()
os.Remove(tmpfile.Name())
defer os.Remove(tmpfile.Name())
storage, err := NewFileStorage(tmpfile.Name())
assert.Nil(t, err)
p := &types.Playground{Id: "aaabbbccc"}
err = storage.PlaygroundPut(p)
assert.Nil(t, err)
expectedDB := &DB{
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{p.Id: p},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{},
UsersByProvider: map[string]string{},
}
var loadedDB *DB
file, err := os.Open(tmpfile.Name())
assert.Nil(t, err)
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&loadedDB)
assert.Nil(t, err)
assert.EqualValues(t, expectedDB, loadedDB)
}
func TestPlaygroundGetAll(t *testing.T) {
p1 := &types.Playground{Id: "aaabbbccc"}
p2 := &types.Playground{Id: "dddeeefff"}
expectedDB := &DB{
Sessions: map[string]*types.Session{},
Instances: map[string]*types.Instance{},
Clients: map[string]*types.Client{},
WindowsInstances: map[string]*types.WindowsInstance{},
LoginRequests: map[string]*types.LoginRequest{},
Users: map[string]*types.User{},
Playgrounds: map[string]*types.Playground{p1.Id: p1, p2.Id: p2},
WindowsInstancesBySessionId: map[string][]string{},
InstancesBySessionId: map[string][]string{},
ClientsBySessionId: map[string][]string{},
UsersByProvider: map[string]string{},
}
tmpfile, err := ioutil.TempFile("", "pwd")
if err != nil {
log.Fatal(err)
}
encoder := json.NewEncoder(tmpfile)
err = encoder.Encode(&expectedDB)
assert.Nil(t, err)
tmpfile.Close()
defer os.Remove(tmpfile.Name())
storage, err := NewFileStorage(tmpfile.Name())
assert.Nil(t, err)
found, err := storage.PlaygroundGetAll()
assert.Nil(t, err)
assert.Equal(t, []*types.Playground{p1, p2}, found)
}

View File

@@ -106,3 +106,15 @@ func (m *Mock) UserGet(id string) (*types.User, error) {
args := m.Called(id)
return args.Get(0).(*types.User), args.Error(1)
}
func (m *Mock) PlaygroundPut(playground *types.Playground) error {
args := m.Called(playground)
return args.Error(0)
}
func (m *Mock) PlaygroundGet(id string) (*types.Playground, error) {
args := m.Called(id)
return args.Get(0).(*types.Playground), args.Error(1)
}
func (m *Mock) PlaygroundGetAll() ([]*types.Playground, error) {
args := m.Called()
return args.Get(0).([]*types.Playground), args.Error(1)
}

View File

@@ -42,4 +42,8 @@ type StorageApi interface {
UserFindByProvider(providerName, providerUserId string) (*types.User, error)
UserPut(user *types.User) error
UserGet(id string) (*types.User, error)
PlaygroundPut(playground *types.Playground) error
PlaygroundGet(id string) (*types.Playground, error)
PlaygroundGetAll() ([]*types.Playground, error)
}