Skip to content

Commit

Permalink
Update auth to improve coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ganglyu committed Jun 17, 2022
1 parent 1fffa3d commit 25c057d
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 14 deletions.
3 changes: 3 additions & 0 deletions gnmi_server/gnoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func (srv *Server) Authenticate(ctx context.Context, req *spb_jwt.AuthenticateRe
return nil, status.Errorf(codes.Unimplemented, "")
}
auth_success, _ := UserPwAuth(req.Username, req.Password)
if srv.config.TestMode == true {
auth_success = true
}
if auth_success {
usr, err := user.Lookup(req.Username)
if err == nil {
Expand Down
83 changes: 74 additions & 9 deletions test/test_gnmi_auth.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,83 @@
from utils import run_cmd, gnmi_get_with_password, gnmi_get_with_jwt
from utils import run_cmd, gnmi_set_with_password, gnmi_set_with_jwt
from utils import gnoi_authenticate, gnoi_refresh_with_jwt

import re
import json
import pytest

@pytest.mark.auth
class TestGNMIAuth:

def test_gnmi_get_with_pwd_neg(self):
ret, msg_list = gnmi_get_with_password([], 'gnmitest', 'wrongpass')
assert ret != 0, "Auth should fail"
assert 'Unauthenticated' in msg_list[0]
def test_gnmi_set_with_pwd_neg(self):
path = '/sonic-db:APPL_DB/DASH_QOS'
value = {
'qos_02': {'bw': '6000', 'cps': '200', 'flows': '101'}
}
update_list = []
text = json.dumps(value)
file_name = 'update.txt'
file_object = open(file_name, 'w')
file_object.write(text)
file_object.close()
update_list = [path + ':@./' + file_name]

ret, msg = gnmi_set_with_password([], update_list, [], 'gnmitest', 'wrongpass')
assert ret != 0, "Auth should fail"
assert 'Unauthenticated' in msg

def test_gnmi_get_with_jwt_neg(self):
jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.ElsKKULlzGtesThefMuj2_a6KIY9L5i2zDrBLHV-e0M'
ret, msg_list = gnmi_get_with_jwt([], jwt)
def test_gnmi_set_with_jwt_neg(self):
path = '/sonic-db:APPL_DB/DASH_QOS'
value = {
'qos_02': {'bw': '6000', 'cps': '200', 'flows': '101'}
}
update_list = []
text = json.dumps(value)
file_name = 'update.txt'
file_object = open(file_name, 'w')
file_object.write(text)
file_object.close()
update_list = [path + ':@./' + file_name]

token = 'InvalidToken.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.ElsKKULlzGtesThefMuj2_a6KIY9L5i2zDrBLHV-e0M'
ret, msg = gnmi_set_with_jwt([], update_list, [], token)
assert ret != 0, "Auth should fail"
assert 'Unauthenticated' in msg_list[0]
assert 'Unauthenticated' in msg

def test_gnmi_set_with_jwt(self):
ret, msg = gnoi_authenticate('gnmitest', 'wrongpass')
assert ret == 0, msg
assert 'access_token' in msg
searchObj = re.search( r'"access_token":"(.*?)"', msg, re.M|re.I)
if searchObj:
token = searchObj.group(1)

path = '/sonic-db:APPL_DB/DASH_QOS'
value = {
'qos_02': {'bw': '6000', 'cps': '200', 'flows': '101'}
}
update_list = []
text = json.dumps(value)
file_name = 'update.txt'
file_object = open(file_name, 'w')
file_object.write(text)
file_object.close()
update_list = [path + ':@./' + file_name]
ret, msg = gnmi_set_with_jwt([], update_list, [], token)
assert ret == 0, msg


@pytest.mark.auth
class TestGNOIAuth:

def test_gnoi_authenticate(self):
ret, msg = gnoi_authenticate('gnmitest', 'wrongpass')
assert ret == 0, msg
assert 'access_token' in msg
searchObj = re.search( r'"access_token":"(.*?)"', msg, re.M|re.I)
if searchObj:
token = searchObj.group(1)

ret, msg = gnoi_refresh_with_jwt(token)
assert ret == 0, msg
assert 'access_token' in msg

5 changes: 0 additions & 5 deletions test/test_gnoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ def test_gnoi_traceroute(self):
assert ret == 0, msg
assert 'ClientStream' in msg

def test_gnoi_setpackage(self):
ret, msg = gnoi_setpackage()
assert ret != 0, 'SetPackage should fail' + msg
assert 'Unimplemented' in msg

def test_gnoi_switchcontrolprocessor(self):
ret, msg = gnoi_switchcontrolprocessor()
assert ret != 0, 'SwitchControlProcessor should fail' + msg
Expand Down
52 changes: 52 additions & 0 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,40 @@ def gnmi_set(delete_list, update_list, replace_list):
return ret, ''
return ret, msg

def gnmi_set_with_password(delete_list, update_list, replace_list, user, password):
path = os.getcwd()
cmd = path + '/build/bin/gnmi_set '
cmd += '-insecure -username %s -password %s '%(user, password)
cmd += '-target_addr 127.0.0.1:8080 '
cmd += '-alsologtostderr '
for delete in delete_list:
cmd += " -delete " + delete
for update in update_list:
cmd += " -update " + update
for replace in replace_list:
cmd += " -replace " + replace
ret, msg = run_cmd(cmd)
if ret == 0:
return ret, ''
return ret, msg

def gnmi_set_with_jwt(delete_list, update_list, replace_list, token):
path = os.getcwd()
cmd = path + '/build/bin/gnmi_set '
cmd += '-insecure -jwt_token ' + token + ' '
cmd += '-target_addr 127.0.0.1:8080 '
cmd += '-alsologtostderr '
for delete in delete_list:
cmd += " -delete " + delete
for update in update_list:
cmd += " -update " + update
for replace in replace_list:
cmd += " -replace " + replace
ret, msg = run_cmd(cmd)
if ret == 0:
return ret, ''
return ret, msg

def gnmi_get(path_list):
path = os.getcwd()
cmd = path + '/build/bin/gnmi_get '
Expand Down Expand Up @@ -192,3 +226,21 @@ def gnoi_switchcontrolprocessor():
ret, msg = run_cmd(cmd)
return ret, msg

def gnoi_authenticate(username, password):
path = os.getcwd()
cmd = path + '/build/bin/gnoi_client '
cmd += '-insecure -target 127.0.0.1:8080 '
cmd += '-module Sonic -rpc authenticate '
cmd += '-jsonin "{\\\"Username\\\":\\\"%s\\\", \\\"Password\\\":\\\"%s\\\"}"'%(username, password)
ret, msg = run_cmd(cmd)
return ret, msg

def gnoi_refresh_with_jwt(token):
path = os.getcwd()
cmd = path + '/build/bin/gnoi_client '
cmd += '-insecure -target 127.0.0.1:8080 '
cmd += '-jwt_token ' + token + ' '
cmd += '-module Sonic -rpc refresh '
ret, msg = run_cmd(cmd)
return ret, msg

0 comments on commit 25c057d

Please sign in to comment.