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

[Task] VX-233 johnlin/resource createdby #204

Merged
merged 43 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6eefbb1
user bats tests
John-Lin Aug 21, 2018
a6bb958
add account/passwotd
John-Lin Aug 21, 2018
88491bb
add jwt auth
John-Lin Aug 21, 2018
6338561
add createdby field into network
John-Lin Jul 30, 2018
7bc4f05
insert user
John-Lin Jul 31, 2018
785e828
service has createdby field
John-Lin Jul 31, 2018
8f8b0ba
fix 403
John-Lin Jul 31, 2018
f4b9073
volume
John-Lin Jul 31, 2018
d2118e6
fix
John-Lin Jul 31, 2018
38d50cf
pod has user field
John-Lin Jul 31, 2018
d8b8f15
use Unauthorized
John-Lin Jul 31, 2018
bdbc836
fix rebase
John-Lin Jul 31, 2018
b35451c
dropdb when failed
John-Lin Aug 7, 2018
95f4595
sudo
John-Lin Aug 21, 2018
37ca7e7
remove cred
John-Lin Aug 21, 2018
84b6998
cred
John-Lin Aug 21, 2018
dd7c371
typos
John-Lin Aug 21, 2018
d4cb65b
test login
John-Lin Aug 21, 2018
fdf20fe
add cred
John-Lin Aug 21, 2018
3397433
fix test
John-Lin Aug 21, 2018
fc02fd4
fix test
John-Lin Aug 22, 2018
8e0c92a
cleanup user data
John-Lin Aug 22, 2018
52e44cc
login when using delete
John-Lin Aug 22, 2018
6258362
rename and comments
John-Lin Aug 22, 2018
c86b182
rename variable
John-Lin Aug 22, 2018
70232f1
typos and doc
John-Lin Aug 22, 2018
f9cdd5c
fix test
John-Lin Aug 22, 2018
ebf20b4
fix test
John-Lin Aug 22, 2018
ee37b37
remove uuid and jwt
John-Lin Aug 22, 2018
fba99d6
update doc
John-Lin Aug 22, 2018
9487f9f
typos
John-Lin Aug 22, 2018
f2d0ea5
add jwt auth httpie plugin
John-Lin Aug 22, 2018
9446a3e
add test
John-Lin Aug 22, 2018
a613ac6
remove uuid package
John-Lin Aug 22, 2018
cbfc0aa
find and insert user when get/list/create
John-Lin Aug 23, 2018
5df9592
typos
John-Lin Aug 23, 2018
35d96fb
ignore finduser error
John-Lin Aug 23, 2018
a5bb373
add ownerid
John-Lin Aug 23, 2018
b7ed738
typos
John-Lin Aug 23, 2018
f1aa9c7
ownerID
John-Lin Aug 23, 2018
a4cf87d
pod ownerID
John-Lin Aug 23, 2018
84fc53d
service owner id
John-Lin Aug 23, 2018
e2dbc66
volume ownerid
John-Lin Aug 23, 2018
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
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ jobs:
- bash <(curl -s https://codecov.io/bash)
- name: "Integration Tests"
install:
- sudo add-apt-repository ppa:duggan/bats --yes
- sudo add-apt-repository ppa:duggan/bats --yes
- sudo apt-get update -q
- sudo apt-get install -qqy httpie bats nfs-kernel-server
- sudo apt-get install -qqy httpie bats nfs-kernel-server
- sudo pip install -U httpie-jwt-auth
- mkdir -p /tmp/nfs
- echo "/tmp/nfs *(rw,sync,no_root_squash)" | sudo tee /etc/exports
- sudo exportfs -r
Expand Down
40 changes: 15 additions & 25 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ Example:
```json
{
"loginCredential":{
"email":"[email protected]",
"username":"[email protected]",
"password":"password"
},
"username":"John Doe",
"displayName":"John Doe",
"firstName":"John",
"lastName":"Doe",
"phoneNumber":"0911111111"
Expand All @@ -87,13 +87,11 @@ Response Data:
```json
{
"id": "5b5b418c760aab15e771bde2",
"uuid": "44b4646a-d009-457c-9fdd-1cc0bf226543",
"jwt": "",
"loginCredential": {
"email": "[email protected]",
"username": "[email protected]",
"password": "$2a$14$XO4OOUCaiTNQHm.ZTzHU5..WwtP2ec2Q2HPPQuMHP1WoXCjXiRrxa"
},
"username": "John Doe",
"displayName": "John Doe",
"role": "user",
"firstName": "John",
"lastName": "Doe",
Expand All @@ -110,7 +108,7 @@ Example:

```json
{
"email":"[email protected]",
"username":"[email protected]",
"password":"password"
}
```
Expand All @@ -135,11 +133,11 @@ role can only be "root", "user", "guest".
```json
{
"loginCredential":{
"email":"[email protected]",
"username":"[email protected]",
"password":"password"
},
"role": "guest",
"username":"John Doe",
"displayName":"John Doe",
"firstName":"John",
"lastName":"Doe",
"phoneNumber":"0911111111"
Expand All @@ -151,13 +149,11 @@ Response Data:
```json
{
"id": "5b5b418c760aab15e771bde2",
"uuid": "44b4646a-d009-457c-9fdd-1cc0bf226543",
"jwt": "",
"loginCredential": {
"email": "[email protected]",
"username": "[email protected]",
"password": "$2a$14$XO4OOUCaiTNQHm.ZTzHU5..WwtP2ec2Q2HPPQuMHP1WoXCjXiRrxa"
},
"username": "John Doe",
"displayName": "John Doe",
"role": "guest",
"firstName": "John",
"lastName": "Doe",
Expand All @@ -181,13 +177,11 @@ Response Data:
[
{
"id": "5b5b4173760aab15e771bde0",
"uuid": "52870ee9-4bfd-44ea-8cca-a9ce7826b1bd",
"jwt": "",
"loginCredential": {
"email": "[email protected]",
"username": "[email protected]",
"password": "$2a$14$CQasyFUsBuqwmmpk/i9t9.9j2BTyPzK3PyWATMgb/7g8do57c9oHe"
},
"username": "John Doe",
"displayName": "John Doe",
"role": "root",
"firstName": "John",
"lastName": "Doe",
Expand All @@ -196,13 +190,11 @@ Response Data:
},
{
"id": "5b5b4184760aab15e771bde1",
"uuid": "a4604f7d-06a8-4226-9792-765e72b14f9c",
"jwt": "",
"loginCredential": {
"email": "[email protected]",
"username": "[email protected]",
"password": "$2a$14$SzULcUvWqsCy6XeelPdsRutCDJkdsrM4mi2HXpXPEaEugV.jJsMNC"
},
"username": "John Doe",
"displayName": "John Doe",
"role": "user",
"firstName": "John",
"lastName": "Doe",
Expand All @@ -211,13 +203,11 @@ Response Data:
},
{
"id": "5b5b418c760aab15e771bde2",
"uuid": "44b4646a-d009-457c-9fdd-1cc0bf226543",
"jwt": "",
"loginCredential": {
"email": "[email protected]",
"username": "[email protected]",
"password": "$2a$14$XO4OOUCaiTNQHm.ZTzHU5..WwtP2ec2Q2HPPQuMHP1WoXCjXiRrxa"
},
"username": "John Doe",
"displayName": "John Doe",
"role": "guest",
"firstName": "John",
"lastName": "Doe",
Expand Down
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,12 @@ src.test-coverage-minikube:
sed -i.bak "s/localhost:9090/$$(minikube ip):30003/g; s/localhost:27017/$$(minikube ip):31717/g" config/testing.json
$(MAKE) src.test-coverage
mv config/testing.json.bak config/testing.json
kubectl exec mongo-0 -n vortex -- mongo vortex_test --eval 'printjson(db.dropDatabase())'

.PHONY: src.test-coverage-vagrant
src.test-coverage-vagrant:
sed -i.bak "s/localhost:9090/172.17.8.100:30003/g; s/localhost:27017/172.17.8.100:31717/g" config/testing.json
$(MAKE) src.test-coverage
mv config/testing.json.bak config/testing.json
kubectl exec mongo-0 -n vortex -- mongo vortex_test --eval 'printjson(db.dropDatabase())'

## check build env #############################

Expand Down
1 change: 1 addition & 0 deletions developers/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sudo mkdir -p "/etc/systemd/system/docker.service.d/"
sudo cp "/tmp/override.conf" "/etc/systemd/system/docker.service.d/override.conf"
sudo apt-get update
sudo apt-get install -y git build-essential nfs-common ntp jq bats httpie
sudo pip install httpie-jwt-auth

# Install Docker
# kubernetes official max validated version: 17.03.2~ce-0~ubuntu-xenial
Expand Down
13 changes: 2 additions & 11 deletions src/entity/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package entity
import (
"time"

"github.com/linkernetworks/vortex/src/errors"
"gopkg.in/mgo.v2/bson"
)

Expand Down Expand Up @@ -37,26 +36,18 @@ type Node struct {
// Network is the structure for Network info
type Network struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
OwnerID bson.ObjectId `bson:"ownerID,omitempty" json:"ownerID" validate:"-"`
Type NetworkType `bson:"type" json:"type" validate:"required"`
IsDPDKPort bool `bson:"isDPDKPort" json:"isDPDKPort" validate:"-"`
Name string `bson:"name" json:"name" validate:"required"`
VlanTags []int32 `bson:"vlanTags" json:"vlanTags" validate:"required,dive,max=4095,min=0"`
BridgeName string `bson:"bridgeName" json:"bridgeName" validate:"-"`
Nodes []Node `bson:"nodes" json:"nodes" validate:"required,dive,required"`
CreatedBy User `json:"createdBy" validate:"-"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty" validate:"-"`
}

// GetCollection - get model mongo collection name.
func (m Network) GetCollection() string {
return NetworkCollectionName
}

// ValidateVLANTags will validate VLAN tags
func ValidateVLANTags(vlanTags []int32) error {
for _, tag := range vlanTags {
if tag < 0 || tag > 4095 {
return errors.NewErrInvalidVLAN("VLAN tag should between 0 and 4095")
}
}
return nil
}
3 changes: 3 additions & 0 deletions src/entity/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type PodVolume struct {
// Pod is the structure for pod info
type Pod struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
OwnerID bson.ObjectId `bson:"ownerID,omitempty" json:"ownerID" validate:"-"`
Name string `bson:"name" json:"name" validate:"required,k8sname"`
Namespace string `bson:"namespace" json:"namespace" validate:"required"`
Labels map[string]string `bson:"labels,omitempty" json:"labels" validate:"required,dive,keys,printascii,endkeys,required,printascii"`
Expand All @@ -71,6 +72,8 @@ type Pod struct {
Capability bool `bson:"capability" json:"capability" validate:"-"`
NetworkType string `bson:"networkType" json:"networkType" validate:"required,eq=host|eq=cluster|eq=custom"`
NodeAffinity []string `bson:"nodeAffinity" json:"nodeAffinity" validate:"required"`
HostNetwork bool `bson:"hostNetwork" json:"hostNetwork" validate:"-"`
CreatedBy User `json:"createdBy" validate:"-"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty" validate:"-"`
}

Expand Down
2 changes: 2 additions & 0 deletions src/entity/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ type ServicePort struct {
// Service is the structure for service
type Service struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
OwnerID bson.ObjectId `bson:"ownerID,omitempty" json:"ownerID" validate:"-"`
Name string `bson:"name" json:"name" validate:"required,k8sname"`
Namespace string `bson:"namespace" json:"namespace" validate:"required"`
Type string `bson:"type" json:"type" validate:"oneof=ClusterIP NodePort"`
Selector map[string]string `bson:"selector" json:"selector" validate:"required"`
Ports []ServicePort `bson:"ports" json:"ports" validate:"required"`
CreatedBy User `json:"createdBy" validate:"-"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty" validate:"-"`
}

Expand Down
6 changes: 2 additions & 4 deletions src/entity/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@ type RegistryBasicAuthCredential struct {

// LoginCredential is the structure for a user login credential
type LoginCredential struct {
Email string `bson:"email" json:"email" validate:"required,email"`
Username string `bson:"username" json:"username" validate:"required,email"`
Password string `bson:"password" json:"password" validate:"required"`
}

// User is the structure for user info
type User struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
UUID string `bson:"uuid" json:"uuid" validate:"required,uuid4"`
JWT string `bson:"jwt" json:"jwt" validate:"-"`
LoginCredential LoginCredential `bson:"loginCredential" json:"loginCredential" validate:"required"`
Username string `bson:"username" json:"username" validate:"required"`
DisplayName string `bson:"displayName" json:"displayName" validate:"required"`
Role string `bson:"role" json:"role" validate:"required,eq=root|eq=user|eq=guest"`
FirstName string `bson:"firstname" json:"firstName" validate:"required"`
LastName string `bson:"lastName" json:"lastName" validate:"required"`
Expand Down
2 changes: 2 additions & 0 deletions src/entity/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ const (
// So the Volume will create a PVC type and connect to a known StorageClass
type Volume struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
OwnerID bson.ObjectId `bson:"ownerID,omitempty" json:"ownerID" validate:"-"`
Name string `bson:"name" json:"name" validate:"required"`
StorageName string `bson:"storageName" json:"storageName" validate:"required"`
AccessMode corev1.PersistentVolumeAccessMode `bson:"accessMode" json:"accessMode" validate:"required"`
Capacity string `bson:"capacity" json:"capacity" validate:"required"`
CreatedBy User `json:"createdBy" validate:"-"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty" validate:"-"`
}

Expand Down
18 changes: 0 additions & 18 deletions src/errors/network.go

This file was deleted.

3 changes: 0 additions & 3 deletions src/networkprovider/ovs_netdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ type userspaceNetworkProvider struct {
}

func (unp userspaceNetworkProvider) CreateNetwork(sp *serviceprovider.Container) error {
if err := entity.ValidateVLANTags(unp.VlanTags); err != nil {
return err
}
for _, node := range unp.Nodes {
nodeIP, err := sp.KubeCtl.GetNodeInternalIP(node.Name)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions src/networkprovider/ovs_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ type kernelspaceNetworkProvider struct {
}

func (knp kernelspaceNetworkProvider) CreateNetwork(sp *serviceprovider.Container) error {
if err := entity.ValidateVLANTags(knp.VlanTags); err != nil {
return err
}
for _, node := range knp.Nodes {
nodeIP, err := sp.KubeCtl.GetNodeInternalIP(node.Name)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/server/backend/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Authenticate(session *mongo.Session, credential entity.LoginCredential) (en
authenticatedUser := entity.User{}
if err := session.FindOne(
entity.UserCollectionName,
bson.M{"loginCredential.email": credential.Email},
bson.M{"loginCredential.username": credential.Username},
&authenticatedUser,
); err != nil {
return entity.User{}, false, err
Expand Down
12 changes: 6 additions & 6 deletions src/server/backend/authenticate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func (suite *AuthenticateTestSuite) SetupSuite() {
user := entity.User{
ID: bson.NewObjectId(),
LoginCredential: entity.LoginCredential{
Email: "[email protected]",
Username: "[email protected]",
Password: hashedPassword,
},
Username: "John Doe",
DisplayName: "John Doe",
FirstName: "John",
LastName: "Doe",
PhoneNumber: "091111l111",
Expand All @@ -55,7 +55,7 @@ func (suite *AuthenticateTestSuite) SetupSuite() {
func (suite *AuthenticateTestSuite) TearDownSuite() {
suite.session.Remove(
entity.UserCollectionName,
"loginCredential.email",
"loginCredential.username",
"[email protected]",
)
}
Expand All @@ -66,18 +66,18 @@ func TestAuthenticateSuite(t *testing.T) {

func (suite *AuthenticateTestSuite) TestAuthenticate() {
CorrectCred := entity.LoginCredential{
Email: "[email protected]",
Username: "[email protected]",
Password: suite.plainTextPassword,
}
user, passed, err := Authenticate(suite.session, CorrectCred)
suite.NoError(err)
suite.True(passed)
suite.Equal(CorrectCred.Email, user.LoginCredential.Email)
suite.Equal(CorrectCred.Username, user.LoginCredential.Username)
}

func (suite *AuthenticateTestSuite) TestFailedAuthenticate() {
WrongCred := entity.LoginCredential{
Email: "[email protected]",
Username: "[email protected]",
Password: "wrongPasswordOX",
}
_, passed, err := Authenticate(suite.session, WrongCred)
Expand Down
4 changes: 2 additions & 2 deletions src/server/backend/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// GenerateToken is for generating token
func GenerateToken(userUUID string, role string) (string, error) {
func GenerateToken(userID string, role string) (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = jwt.MapClaims{
// issuer of the claim
Expand All @@ -17,7 +17,7 @@ func GenerateToken(userUUID string, role string) (string, error) {
// user role
"role": role,
// the subject of this token. This is the user associated with the relevant action
"sub": userUUID,
"sub": userID,
}
return token.SignedString([]byte(SecretKey))
}
19 changes: 19 additions & 0 deletions src/server/backend/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package backend

import (
"github.com/linkernetworks/mongo"
"github.com/linkernetworks/vortex/src/entity"
"gopkg.in/mgo.v2/bson"
)

func FindUserByID(session *mongo.Session, ID bson.ObjectId) (entity.User, error) {
var user entity.User
if err := session.FindOne(
entity.UserCollectionName,
bson.M{"_id": ID},
&user,
); err != nil {
return entity.User{}, err
}
return user, nil
}
Loading