Commit 23f85e6a authored by Loïck Bonniot's avatar Loïck Bonniot

[p/contract] Add authentication in postContract

parent d6abf53a
Pipeline #271 passed with stage
-----BEGIN CERTIFICATE-----
MIIBkDCCAToCCQDSSWVk2vWTdjANBgkqhkiG9w0BAQsFADBNMQswCQYDVQQGEwJG
UjENMAsGA1UECgwEREZTUzEbMBkGA1UECwwSREZTUyBQbGF0Zm9ybSB2MC4xMRIw
EAYDVQQDDAlsb2NhbGhvc3QwIBcNMTYwMTI2MTUzNjUzWhgPNDQ4MDAzMDgxNTM2
NTNaME8xCzAJBgNVBAYTAkZSMQ0wCwYDVQQKDARERlNTMRkwFwYDVQQLDBBERlNT
IENsaWVudCB2MC4xMRYwFAYDVQQDDA10ZXN0QHRlc3QuY29tMFwwDQYJKoZIhvcN
AQEBBQADSwAwSAJBAMxHU0NP/elQbmM5HDZS5iWXr4wllaJ2bWWD0cZPI1p+jty0
wwkKwxEklPGZCDWq1+/C4EawaqMrtZW4HQVxdu8CAwEAATANBgkqhkiG9w0BAQsF
AANBACl2/KBGR8N4qzpNecr1yDdyfyE4nGYgr8aktAeHHNWFg53q3/VHokK0jEus
iM6sQlvDCoaE01s6gXrarE+APfU=
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,3AEE90521B16D55A
3848I///OSqUkY2nNmjP+CRz+mnwg1gTImYfwTQDvGgTeMBZyYh9N7wTir10/3rJ
AD9hu6SgkZ1N9/jV7m4rVW8uJvBFyL+ZIq2aEvt2lpWwXF4Re9OeZGHrQSpVlLSD
Kv3ZTQyEaTrQ35LgfMtd6UEd5YO/I+pv3ggUtxaNCJeu2hDsEpBkOhWfslCeidZ7
m3wR7I3zyXr2rHMO455yusuCrg+puel8/FSMwhGXTOi2GA2C//rRolGn31GZJD8o
STTjKNbxXzo6wuuuoiubSycOw6iLb/P9S05/CKkcV99cZRW2wjIf2BecXjC1sezP
hxQs83QuqN06NIfhARGgfibYPny5AtZRP2PyU/JOIgvwLrNL5Z8VjioF0+/ZfXCi
MhNuq+NLSa0WujUwWEzn4g3EJzrylFx6/RKQju7Y23A=
-----END RSA PRIVATE KEY-----
...@@ -3,7 +3,6 @@ package contract_test ...@@ -3,7 +3,6 @@ package contract_test
import ( import (
"crypto/sha512" "crypto/sha512"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
"time" "time"
...@@ -55,20 +54,34 @@ func dropDataset() { ...@@ -55,20 +54,34 @@ func dropDataset() {
} }
func clientTest(t *testing.T) api.PlatformClient { func clientTest(t *testing.T) api.PlatformClient {
path := filepath.Join(os.Getenv("GOPATH"), "src", "dfss", "dfssp", "testdata", "dfssp_rootCA.pem") // TODO if anyone needs this function in another test suite, please put it in a separate file
CAData, err := ioutil.ReadFile(path) // to avoid code duplication
caData, _ := ioutil.ReadFile(filepath.Join("..", "testdata", "dfssp_rootCA.pem"))
certData, _ := ioutil.ReadFile(filepath.Join("..", "..", "dfssc", "testdata", "cert.pem"))
keyData, _ := ioutil.ReadFile(filepath.Join("..", "..", "dfssc", "testdata", "key.pem"))
ca, _ := auth.PEMToCertificate(caData)
cert, _ := auth.PEMToCertificate(certData)
key, _ := auth.EncryptedPEMToPrivateKey(keyData, "password")
conn, err := net.Connect("localhost:9090", cert, key, ca)
if err != nil { if err != nil {
t.Fatal("Unable to load CA file:", err) t.Fatal("Unable to connect:", err)
} }
CA, err := auth.PEMToCertificate(CAData) return api.NewPlatformClient(conn)
}
conn, err := net.Connect("localhost:9090", nil, nil, CA) func TestAddContractBadAuth(t *testing.T) {
caData, _ := ioutil.ReadFile(filepath.Join("..", "testdata", "dfssp_rootCA.pem"))
ca, _ := auth.PEMToCertificate(caData)
conn, err := net.Connect("localhost:9090", nil, nil, ca)
if err != nil { if err != nil {
t.Fatal("Unable to connect:", err) t.Fatal("Unable to connect:", err)
} }
client := api.NewPlatformClient(conn)
return api.NewPlatformClient(conn) errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{})
assert.Equal(t, nil, err)
assert.Equal(t, api.ErrorCode_BADAUTH, errorCode.Code)
} }
func TestAddContract(t *testing.T) { func TestAddContract(t *testing.T) {
...@@ -82,12 +95,8 @@ func TestAddContract(t *testing.T) { ...@@ -82,12 +95,8 @@ func TestAddContract(t *testing.T) {
Signer: []string{user1.Email, user2.Email}, Signer: []string{user1.Email, user2.Email},
Comment: "ContractComment", Comment: "ContractComment",
}) })
if err != nil { assert.Equal(t, nil, err)
t.Fatal(err) assert.Equal(t, api.ErrorCode_SUCCESS, errorCode.Code)
}
if errorCode.Code != api.ErrorCode_SUCCESS {
t.Fatal("Unexpected errorCode:", errorCode)
}
// Check database content // Check database content
var contracts []entities.Contract var contracts []entities.Contract
......
...@@ -47,6 +47,12 @@ func (s *platformServer) Unregister(ctx context.Context, in *api.Empty) (*api.Er ...@@ -47,6 +47,12 @@ func (s *platformServer) Unregister(ctx context.Context, in *api.Empty) (*api.Er
// //
// Handle incoming PostContractRequest messages // Handle incoming PostContractRequest messages
func (s *platformServer) PostContract(ctx context.Context, in *api.PostContractRequest) (*api.ErrorCode, error) { func (s *platformServer) PostContract(ctx context.Context, in *api.PostContractRequest) (*api.ErrorCode, error) {
cn := net.GetCN(&ctx)
if len(cn) == 0 {
return &api.ErrorCode{Code: api.ErrorCode_BADAUTH}, nil
}
builder := contract.NewContractBuilder(s.DB, in) builder := contract.NewContractBuilder(s.DB, in)
return builder.Execute(), nil return builder.Execute(), nil
} }
......
-----BEGIN RSA PRIVATE KEY----- -----BEGIN RSA PRIVATE KEY-----
MIIBOQIBAAJBAKC2KQ/06wgb3+oW8mH/G7dtYam7r38p9AHqRQ3Bs4OKYDGFkBSt MIIBOwIBAAJBAMGAgCtkRLePYFRTUN0V/0v/6phm0guHGS6f0TkSEas4CGZTKFJV
ekUjDW+5cD30SXjZmPLbb4CwM94UeGA3xykCAwEAAQJAGoLSNUKmvtiyUFBmvYmy TBksMGIBtfyYw3XQx2bO8myeypDN5nV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NjAxMjYxNTM2NTNaGA80NDgwMDMw
ODEwWjBKMQ8wDQYDVQQGEwZGcmFuY2UxDTALBgNVBAoTBERGU1MxFDASBgNVBAsT ODE1MzY1M1owTTELMAkGA1UEBhMCRlIxDTALBgNVBAoMBERGU1MxGzAZBgNVBAsM
C0lOU0EgUmVubmVzMRIwEAYDVQQDEwlsb2NhbGhvc3QwXDANBgkqhkiG9w0BAQEF EkRGU1MgUGxhdGZvcm0gdjAuMTESMBAGA1UEAwwJbG9jYWxob3N0MFwwDQYJKoZI
AANLADBIAkEAoLYpD/TrCBvf6hbyYf8bt21hqbuvfyn0AepFDcGzg4pgMYWQFK16 hvcNAQEBBQADSwAwSAJBAMGAgCtkRLePYFRTUN0V/0v/6phm0guHGS6f0TkSEas4
RSMNb7lwPfRJeNmY8ttvgLAz3hR4YDfHKQIDAQABoxMwETAPBgNVHRMBAf8EBTAD CGZTKFJVTBksMGIBtfyYw3XQx2bO8myeypDN5nV05DcCAwEAAaNQME4wHQYDVR0O
AQH/MA0GCSqGSIb3DQEBCwUAA0EAiLMTR42DBa8lXs5q1GTQ6UvTMGlnkX90Ua+W BBYEFO09nxx5/qeLK5Wig1+3kg66gn/mMB8GA1UdIwQYMBaAFO09nxx5/qeLK5Wi
/JZ1chC6hrUHB9kr3Zo1bz9S0xTouR8IfcxhWEcD/OoBvfW7Vw== g1+3kg66gn/mMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADQQCqNSH+rt/Z
ru2rkabLiHOGjI+AenSOvqWZ2dWAlLksYcyuQHKwjGWgpmqkiQCnkIDwIxZvu69Y
OBz0ASFn7eym
-----END CERTIFICATE----- -----END CERTIFICATE-----
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment