Skip to content

Commit

Permalink
Fix win style
Browse files Browse the repository at this point in the history
  • Loading branch information
ganglyu committed Sep 24, 2022
1 parent 0ed45ac commit 474b344
Show file tree
Hide file tree
Showing 15 changed files with 4,165 additions and 4,180 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ check_gotest:
sudo mkdir -p /usr/models/yang || true
sudo find $(MGMT_COMMON_DIR)/models -name '*.yang' -exec cp {} /usr/models/yang/ \;
sudo $(GO) test -coverprofile=coverage-config.txt -covermode=atomic -v github.com/sonic-net/sonic-gnmi/sonic_db_config
sudo $(GO) test -coverprofile=coverage-gnmi.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/gnmi_server
sudo $(GO) test -coverprofile=coverage-gnmi.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/gnmi_server -coverpkg ../...
sudo $(GO) test -coverprofile=coverage-dialcout.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/dialout/dialout_client
sudo $(GO) test -coverprofile=coverage-data.txt -covermode=atomic -mod=vendor -v github.com/sonic-net/sonic-gnmi/sonic_data_client
sudo $(GO) test -coverprofile=coverage-dbus.txt -covermode=atomic -mod=vendor -v github.com/sonic-net/sonic-gnmi/sonic_service_client
Expand Down
22 changes: 3 additions & 19 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ stages:
sudo dpkg -i ../target/debs/bullseye/libnl-nf-3-200_*.deb
sudo dpkg -i ../target/debs/bullseye/libhiredis0.14_*.deb
sudo dpkg -i ../target/debs/bullseye/libhiredis-dev_*.deb
sudo dpkg -i ../target/debs/bullseye/libswsscommon_1.0.0_amd64.deb
sudo dpkg -i ../target/debs/bullseye/libswsscommon-dev_1.0.0_amd64.deb
sudo dpkg -i ../target/debs/bullseye/python3-swsscommon_1.0.0_amd64.deb
displayName: "Install libswsscommon dependencies"
- script: |
Expand All @@ -113,25 +116,6 @@ stages:
sudo apt-get install -y dotnet-sdk-5.0
displayName: "Install .NET CORE"
- task: DownloadPipelineArtifact@2
inputs:
source: specific
project: build
pipeline: Azure.sonic-swss-common
artifact: sonic-swss-common
runVersion: 'latestFromBranch'
runBranch: 'refs/heads/master'
displayName: "Download sonic-swss-common"

- script: |
set -ex
# LIBSWSSCOMMON
sudo dpkg -i libswsscommon_1.0.0_amd64.deb
sudo dpkg -i libswsscommon-dev_1.0.0_amd64.deb
sudo dpkg -i python3-swsscommon_1.0.0_amd64.deb
workingDirectory: $(Pipeline.Workspace)/
displayName: 'Install libswsscommon package'
- script: |
set -ex
ls -l
Expand Down
196 changes: 98 additions & 98 deletions common_utils/context.go
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
package common_utils

import (
"context"
"fmt"
"sync/atomic"
)


// AuthInfo holds data about the authenticated user
type AuthInfo struct {
// Username
User string
AuthEnabled bool
// Roles
Roles []string
}

// RequestContext holds metadata about REST request.
type RequestContext struct {

// Unique reqiest id
ID string

// Auth contains the authorized user information
Auth AuthInfo

//Bundle Version is the release yang models version.
BundleVersion *string
}

type contextkey int

const requestContextKey contextkey = 0

// Request Id generator
var requestCounter uint64

var CountersName = [...]string {
"GNMI get",
"GNMI get fail",
"GNMI set",
"GNMI set fail",
"GNOI reboot",
"DBUS",
"DBUS fail",
"DBUS apply patch db",
"DBUS apply patch yang",
"DBUS create checkpoint",
"DBUS delete checkpoint",
"DBUS config save",
"DBUS config reload",
}

var globalCounters [len(CountersName)]uint64


// GetContext function returns the RequestContext object for a
// gRPC request. RequestContext is maintained as a context value of
// the request. Creates a new RequestContext object is not already
// available.
func GetContext(ctx context.Context) (*RequestContext, context.Context) {
cv := ctx.Value(requestContextKey)
if cv != nil {
return cv.(*RequestContext), ctx
}

rc := new(RequestContext)
rc.ID = fmt.Sprintf("TELEMETRY-%v", atomic.AddUint64(&requestCounter, 1))

ctx = context.WithValue(ctx, requestContextKey, rc)
return rc, ctx
}

func GetUsername(ctx context.Context, username *string) {
rc, _ := GetContext(ctx)
if rc != nil {
*username = rc.Auth.User
}
}

func InitCounters() {
for i := 0; i < len(CountersName); i++ {
globalCounters[i] = 0
}
SetMemCounters(&globalCounters)
}

func IncCounter(name string) {
for i := 0; i < len(CountersName); i++ {
if CountersName[i] == name {
atomic.AddUint64(&globalCounters[i], 1)
break
}
}
SetMemCounters(&globalCounters)
}
package common_utils

import (
"context"
"fmt"
"sync/atomic"
)


// AuthInfo holds data about the authenticated user
type AuthInfo struct {
// Username
User string
AuthEnabled bool
// Roles
Roles []string
}

// RequestContext holds metadata about REST request.
type RequestContext struct {

// Unique reqiest id
ID string

// Auth contains the authorized user information
Auth AuthInfo

//Bundle Version is the release yang models version.
BundleVersion *string
}

type contextkey int

const requestContextKey contextkey = 0

// Request Id generator
var requestCounter uint64

var CountersName = [...]string {
"GNMI get",
"GNMI get fail",
"GNMI set",
"GNMI set fail",
"GNOI reboot",
"DBUS",
"DBUS fail",
"DBUS apply patch db",
"DBUS apply patch yang",
"DBUS create checkpoint",
"DBUS delete checkpoint",
"DBUS config save",
"DBUS config reload",
}

var globalCounters [len(CountersName)]uint64


// GetContext function returns the RequestContext object for a
// gRPC request. RequestContext is maintained as a context value of
// the request. Creates a new RequestContext object is not already
// available.
func GetContext(ctx context.Context) (*RequestContext, context.Context) {
cv := ctx.Value(requestContextKey)
if cv != nil {
return cv.(*RequestContext), ctx
}

rc := new(RequestContext)
rc.ID = fmt.Sprintf("TELEMETRY-%v", atomic.AddUint64(&requestCounter, 1))

ctx = context.WithValue(ctx, requestContextKey, rc)
return rc, ctx
}

func GetUsername(ctx context.Context, username *string) {
rc, _ := GetContext(ctx)
if rc != nil {
*username = rc.Auth.User
}
}

func InitCounters() {
for i := 0; i < len(CountersName); i++ {
globalCounters[i] = 0
}
SetMemCounters(&globalCounters)
}

func IncCounter(name string) {
for i := 0; i < len(CountersName); i++ {
if CountersName[i] == name {
atomic.AddUint64(&globalCounters[i], 1)
break
}
}
SetMemCounters(&globalCounters)
}

84 changes: 42 additions & 42 deletions gnmi_server/basicAuth.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
package gnmi

import (
"github.com/sonic-net/sonic-gnmi/common_utils"
"github.com/golang/glog"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

func BasicAuthenAndAuthor(ctx context.Context) (context.Context, error) {
rc, ctx := common_utils.GetContext(ctx)
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return ctx, status.Errorf(codes.Unknown, "Invalid context")
}

var username string
var passwd string
if username_a, ok := md["username"]; ok {
username = username_a[0]
} else {
return ctx, status.Errorf(codes.Unauthenticated, "No Username Provided")
}

if passwd_a, ok := md["password"]; ok {
passwd = passwd_a[0]
} else {
return ctx, status.Errorf(codes.Unauthenticated, "No Password Provided")
}
if err := PopulateAuthStruct(username, &rc.Auth, nil); err != nil {
glog.Infof("[%s] Failed to retrieve authentication information; %v", rc.ID, err)
return ctx, status.Errorf(codes.Unauthenticated, "")
}
auth_success, _ := UserPwAuth(username, passwd)
if auth_success == false {
return ctx, status.Errorf(codes.PermissionDenied, "Invalid Password")
}

return ctx, nil
}
package gnmi

import (
"github.com/sonic-net/sonic-gnmi/common_utils"
"github.com/golang/glog"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

func BasicAuthenAndAuthor(ctx context.Context) (context.Context, error) {
rc, ctx := common_utils.GetContext(ctx)
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return ctx, status.Errorf(codes.Unknown, "Invalid context")
}

var username string
var passwd string
if username_a, ok := md["username"]; ok {
username = username_a[0]
} else {
return ctx, status.Errorf(codes.Unauthenticated, "No Username Provided")
}

if passwd_a, ok := md["password"]; ok {
passwd = passwd_a[0]
} else {
return ctx, status.Errorf(codes.Unauthenticated, "No Password Provided")
}
if err := PopulateAuthStruct(username, &rc.Auth, nil); err != nil {
glog.Infof("[%s] Failed to retrieve authentication information; %v", rc.ID, err)
return ctx, status.Errorf(codes.Unauthenticated, "")
}
auth_success, _ := UserPwAuth(username, passwd)
if auth_success == false {
return ctx, status.Errorf(codes.PermissionDenied, "Invalid Password")
}

return ctx, nil
}
Loading

0 comments on commit 474b344

Please sign in to comment.