Skip to content

Commit dbf1093

Browse files
Translib support for authorization, yang versioning and Delete flag (#21)
Translib support for user authorization, yang versioning and Delete flag to indicate if the object needs to be deleted on last field delete needed for CLI
1 parent 80f369e commit dbf1093

11 files changed

+642
-17
lines changed

debian/sonic-mgmt-common.install

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ models/yang/sonic/*.yang usr/models/yang
66
models/yang/sonic/common/*.yang usr/models/yang
77
models/yang/annotations/*.yang usr/models/yang
88
config/transformer/models_list usr/models/yang
9+
models/yang/version.xml usr/models/yang
910

1011
# CVL files
1112
build/cvl/schema usr/sbin

models/yang/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# YANG directory
2+
3+
## Directory structure
4+
5+
yang/ --> Standard YANGs
6+
|-- annotations/ --> Transformer annotations
7+
|-- common/ --> Dependencies for standard YANGs
8+
|-- extensions/ --> Extenstions for standard YANGs
9+
|-- sonic/ --> SONiC yangs
10+
|-- testdata/ --> Test YANGs - ignored
11+
`-- version.xml --> YANG bundle version configuration file
12+
13+
All supported standard YANG files (OpenConfig and IETF) are kept in this **yang** directory. Usual practice is to keep only top level YANG module here and keep dependent YANGs, submodules in **yang/common** directory.
14+
15+
Example: openconfig-platform.yang is kept in top **yang** directory and openconfig-platform-types.yang in **yang/common** directory.
16+
17+
All extenstion YANGs **MUST** be kept in **yang/extensions** directory.
18+
19+
## version.xml
20+
21+
version.xml file maintains the yang bundle version number in **Major.Minor.Patch** format.
22+
It is the collective version number for all the YANG modules defined here.
23+
**UPDATE THIS VERSION NUMBER FOR EVERY YANG CHANGE.**
24+
25+
**Major version** should be incremented if YANG model is changed in a non backward compatible manner.
26+
Such changes should be avoided.
27+
28+
* Delete, rename or relocate data node
29+
* Change list key attributes
30+
* Change data type of a node to an incompatible type
31+
* Change leafref target
32+
33+
**Minor version** should be incremented if the YANG change modifies the API in a backward
34+
compatible way. Patch version should be reset to 0.
35+
Candidate YANG changes for this category are:
36+
37+
* Add new YANG module
38+
* Add new YANG data nodes
39+
* Mark a YANG data node as deprecated
40+
* Change data type of a node to a compatible type
41+
* Add new enum or identity
42+
43+
**Patch version** should incremented for cosmetic fixes that do not change YANG API.
44+
Candidate YANG changes for this category are:
45+
46+
* Change description, beautification.
47+
* Expand pattern or range of a node to wider set.
48+
* Change must expression to accept more cases.
49+
* Error message or error tag changes.
50+
51+

models/yang/version.xml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<version-config>
2+
<!--
3+
yang-bundle-version configuration indicates the version
4+
for the collection of all yang modules.
5+
6+
Update the version numbers here for every yang change.
7+
8+
Bump up MAJOR version only if the yang change are not
9+
backward compatible.
10+
+ Renaming or relocating of data nodes
11+
+ Deleting unsupported configs
12+
+ Changing list key attributes
13+
+ Incompatible data type changes
14+
+ Changing leafref target
15+
16+
Bump up MINOR version number for all backward compatible
17+
API changes.
18+
+ Add new config node
19+
+ Data type changes like pattern, range (that are backward compatibile)
20+
+ Adding new enum/identity
21+
22+
Bump up PATCH number for cosmetic fixes that do not affect any API
23+
+ Description changes, beautification
24+
+ Must expression and validations that are backward compatibile
25+
+ error-tag, error-message
26+
+ max-elements, min-elements
27+
+ Mark a node as deprecated
28+
-->
29+
<yang-bundle-version>
30+
<Major>1</Major>
31+
<Minor>0</Minor>
32+
<Patch>0</Patch>
33+
</yang-bundle-version>
34+
35+
</version-config>
36+

translib/app_interface.go

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ type appOptions struct {
6363
// 0 indicates unlimited depth.
6464
// Valid for GET API only.
6565
depth uint
66+
67+
// deleteEmptyEntry indicates if the db entry should be deleted upon
68+
// deletion of last field. This is a non standard option.
69+
deleteEmptyEntry bool
6670
}
6771

6872
//map containing the base path to app module info

translib/authorize.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// //
3+
// Copyright 2019 Broadcom. The term Broadcom refers to Broadcom Inc. and/or //
4+
// its subsidiaries. //
5+
// //
6+
// Licensed under the Apache License, Version 2.0 (the "License"); //
7+
// you may not use this file except in compliance with the License. //
8+
// You may obtain a copy of the License at //
9+
// //
10+
// http://www.apache.org/licenses/LICENSE-2.0 //
11+
// //
12+
// Unless required by applicable law or agreed to in writing, software //
13+
// distributed under the License is distributed on an "AS IS" BASIS, //
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
15+
// See the License for the specific language governing permissions and //
16+
// limitations under the License. //
17+
// //
18+
////////////////////////////////////////////////////////////////////////////////
19+
20+
/*
21+
Package translib defines the functions to be used to authorize
22+
23+
an incoming user. It also includes caching of the UserDB data
24+
25+
needed to authorize the user.
26+
27+
*/
28+
29+
package translib
30+
31+
func isAuthorizedForSet(req SetRequest) bool {
32+
if !req.AuthEnabled {
33+
return true
34+
}
35+
for _, r := range req.User.Roles {
36+
if r == "admin" {
37+
return true
38+
}
39+
}
40+
return false
41+
}
42+
43+
func isAuthorizedForBulk(req BulkRequest) bool {
44+
if !req.AuthEnabled {
45+
return true
46+
}
47+
for _, r := range req.User.Roles {
48+
if r == "admin" {
49+
return true
50+
}
51+
}
52+
return false
53+
}
54+
55+
func isAuthorizedForGet(req GetRequest) bool {
56+
if !req.AuthEnabled {
57+
return true
58+
}
59+
return true
60+
}
61+
62+
func isAuthorizedForSubscribe(req SubscribeRequest) bool {
63+
if !req.AuthEnabled {
64+
return true
65+
}
66+
return true
67+
}
68+
69+
func isAuthorizedForIsSubscribe(req IsSubscribeRequest) bool {
70+
if !req.AuthEnabled {
71+
return true
72+
}
73+
return true
74+
}
75+
76+
func isAuthorizedForAction(req ActionRequest) bool {
77+
if !req.AuthEnabled {
78+
return true
79+
}
80+
return true
81+
}

translib/tlerr/app_errors.go

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ type NotSupportedError errordata
4545
// InternalError indicates a generic error during app execution.
4646
type InternalError errordata
4747

48+
// AuthorizationError indicates the user is not authorized for an operation.
49+
type AuthorizationError errordata
50+
4851
/////////////
4952

5053
func (e InvalidArgsError) Error() string {
@@ -90,3 +93,8 @@ func (e InternalError) Error() string {
9093
func New(msg string, args ...interface{}) InternalError {
9194
return InternalError{Format: msg, Args: args}
9295
}
96+
97+
func (e AuthorizationError) Error() string {
98+
return p.Sprintf(e.Format, e.Args...)
99+
}
100+

translib/tlerr/tlerr.go

+11
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,14 @@ type TranslibSyntaxValidationError struct {
101101
func (e TranslibSyntaxValidationError) Error() string {
102102
return p.Sprintf("%s", e.ErrorStr)
103103
}
104+
105+
type TranslibUnsupportedClientVersion struct {
106+
ClientVersion string
107+
ServerVersion string
108+
ServerBaseVersion string
109+
}
110+
111+
func (e TranslibUnsupportedClientVersion) Error() string {
112+
return p.Sprintf("Unsupported client version %s", e.ClientVersion)
113+
}
114+

0 commit comments

Comments
 (0)