-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinspect_return_value.py
171 lines (125 loc) · 4.46 KB
/
inspect_return_value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
<Program Name>
inspect_return_value.py
<Author>
Sachit Malik <[email protected]>
<Started>
June 6, 2017
<Copyright>
See LICENSE for licensing information.
<Purpose>
Inspections constitute an important part of in-toto. In this script,
we inspect the return value of a step. The user supplies an integer
to the program(as compared to which the user wants to test various
boolean operators), along with path to the corresponding link file
(which link file he wants to test in), and also a bash style boolean
operator.
Suppose the link file is located at /user/abc/def/package.45fe325.link
and the user wants to check whether for the corresponding step (hence the
link file), the corresponding return field contains an integer less than 5.
The usage would be as follows:
python inspect_return_value.py -l /user/abc/def/package.45fe325.link -o lt 5
Please note: the various bash style boolean commands are listed as follows-
* bash-style-boolean-operator
eq ... equal
ne ... not equal
lt ... less than
le ... less than or equal
gt ... greater than
ge ... greater than or equal
General usage:
python inspect_return_value.py -l <path/to/the/link/file> -o <operator>
<integer>
"""
import os
import sys
import argparse
import in_toto.log
from in_toto.models.link import Link as link_import
import securesystemslib.exceptions
def inspect_return_value(link, operator, integer):
"""
<Purpose>
A function which performs the inspection as described above depending
on various arguments.
<Arguments>
link:
the path to the link file
operator:
bash style boolean operator
integer:
the integer to which the return value should be compared
Note: the comparison happens treating the integer as a consequent,
for example: given "lt" as an operator, it will inspect whether
(the return value) <= (the given integer)
<Exceptions>
None
<Returns>
Boolean
"""
imported_link = link_import.read_from_file(link)
# Fetch the return value from the byproducts field
return_value = imported_link.byproducts.get("return-value")
if return_value is None:
raise Exception("No return value found in the link metadata")
if operator == 'eq':
return integer == return_value
elif operator == 'ne':
return integer != return_value
elif operator == 'lt':
return integer > return_value
elif operator == 'le':
return integer >= return_value
elif operator == 'gt':
return integer < return_value
elif operator == 'ge':
return integer <= return_value
else:
raise Exception(
"Invalid operator {}. Valid operators: eq | ne | lt | le | gt | "
"ge".format(operator))
def parse_args():
"""
<Purpose>
A function which parses the user supplied arguments.
<Arguments>
None
<Exceptions>
None
<Returns>
Parsed arguments (args object)
"""
parser = argparse.ArgumentParser(
description="Inspects the return value of a step")
in_toto_args = parser.add_argument_group("in-toto-inspection options")
in_toto_args.add_argument("-l", "--link", type=str, required=True,
help="Path to the link file to be inspected",
metavar="<Path to link metadata>")
in_toto_args.add_argument("-o", "--operator", choices=['eq', 'ne',
'lt', 'le', 'gt', 'ge'], type=str,
required=True, help="The logical operator "
"used to compare the return value with given "
"int")
in_toto_args.add_argument("integer", type=int,
help="The integer to which the return value "
"should be compared", metavar="<Integer to "
"compare>")
args = parser.parse_args()
args.operator = args.operator.lower()
return args
def main():
"""
First calls parse_args() to parse the arguments and then calls
inspect_return_value to inspect the return value.
"""
args = parse_args()
try:
if inspect_return_value(args.link, args.operator, args.integer):
sys.exit(0)
else:
sys.exit(1)
except Exception as e:
print('The following error occured', e)
sys.exit(2)
if __name__ == "__main__":
main()