-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
4,165 additions
and
4,180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.