aboutsummaryrefslogtreecommitdiffstats
path: root/bin/acme/srtool_defect.py
blob: 2cf58375d28abd6f857d5f1c236ccbb1379521e1 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python3
#
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# Security Response Tool Commandline Tool
#
# Copyright (C) 2018       Wind River Systems
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

### Usage Examples (run from top level directory)
# Updating Defect System Issues:   ./bin/acme/srtool_defect.py -U


import os
import sys
import re
import csv
import xml.etree.ElementTree as ET
import argparse
import sqlite3
import subprocess
import json
from time import sleep

# load the srt.sqlite schema indexes
dir_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.insert(0, dir_path)
from common.srt_schema import ORM

srtDbName = 'srt.sqlite'
srtErrorLog = 'srt_errors.txt'

#################################
# Helper methods
#

verbose = False

def debugMsg(msg):
    if verbose:
        print(msg)

overrides = {}

def set_override(key,value=None):
    if not value is None:
        overrides[key] = value
    elif key in os.environ.keys():
        overrides[key] = 'yes' if os.environ[key].startswith('1') else 'no'
    else:
        overrides[key] = 'no'
    if 'yes' == overrides[key]:
        print("OVERRIDE: %s = %s" % (key,overrides[key]))

def get_override(key):
    if key in overrides.keys():
        return 'yes' == overrides[key]
    return False

def srt_error_log(msg):
    f1=open(srtErrorLog, 'a')
    f1.write("|" + msg + "|\n" )
    f1.close()

def get_tag_key(tag,key):
    d = json.loads(tag)
    return d[key]

#################################
# class to hold fields of a Defect System issues
#

class Defect:
    project = ''
    product_id = ''

    def __init__(self,*args, **kwargs):
        self.reset()

    # reset all but persistent project info
    def reset(self):
        self.id = -1
        self.name = ''
        self.summary = ''
        self.url = ''
        self.priority = -1
        self.status = ''
        self.resolution = 'Unresolved'
        self.publish = ''

        # Release name
        self.release_version = ''

        self.date_created = ''
        self.date_updated = ''

        # Extra fields
        self.cve_status = ''
        self.vi_status  = ''
        self.vi_outcome  = ''


#################################
# Import Defect System defects
#

#################################
# Defect System list update
#

#################################
# Defect System add to investigation
#

#################################
# defect_del_from_defect_db
#

#################################
# New defect: simulation
#

def new_defect_name(product_prefix):
    conn = sqlite3.connect(srtDbName)
    cur = conn.cursor()

    sql = "SELECT * FROM orm_srtsetting WHERE name='current_defect_simulation_index'"
    cvi = cur.execute(sql).fetchone()
    if not cvi:
        index = 100
        sql = '''INSERT INTO orm_srtsetting (name, helptext, value) VALUES (?,?,?)'''
        cur.execute(sql, ('current_defect_simulation_index', '', index))
    else:
        index = int(cvi[ORM.SRTSETTING_VALUE]) + 1
        sql = '''UPDATE orm_srtsetting SET value=? WHERE id = ?'''
        cur.execute(sql, (index, cvi[ORM.SRTSETTING_ID]))
    conn.commit() #commit to db
    conn.close()

    defect_name = "DEFECT-%s-%05d" % (product_prefix,index)
    return defect_name

# Example translation of SRTool priority text to the Defect tool text
def translate_priority_srt_to_defect(s):
    Priority = (
        ('Undefined', 'None'),
        ('Minor', 'P4'),
        ('Low', 'P3'),
        ('Medium', 'P2'),
        ('High', 'P1'),
    )
    for i in range(len(Priority)):
        if s == Priority[i][0]:
            return str(Priority[i][1])
    print("ERROR: unknown priority string '%s'" % (s))
    srt_error_log("ERROR: unknown priority string '%s'" % (s))
    return 'None'

def defect_new_defect(product_defect_tags,summary,cve_list,description,reason,priority,components,link):
    #srt_error_log("NEW DEFECT:%s|%s|%s|%s|%s|%s|%s|%s" % (product_defect_tags,summary,cve_list,description,reason,priority,components,link))

    # Translate the SRTool priority to Jira priority
    priority = translate_priority_srt_to_defect(priority)

    product_defect_prefix = get_tag_key(product_defect_tags,'key')
    defect_name = new_defect_name(product_defect_prefix)
    print("CREATED:%s,%s/browse/%s" % (defect_name,link,defect_name))


#################################
# Init/Update from Defect System status
#

#################################
# main loop
#

def main(argv):
    global force_update
    global verbose
    global master_log
    global srt_user
    global srt_passwd

    parser = argparse.ArgumentParser(description='srtool_defect.py: Manage the SRTool to Defect System connection')
    parser.add_argument('--init-defect', '-i', action='store_const', const='init_defect', dest='command', help='Init and import Defect System states and update defects')
    parser.add_argument('--update-defect', '-u', action='store_const', const='update_defect', dest='command', help='Import Defect System states and update defects')
    parser.add_argument('--update-defect-list', '-l', dest='defect_update_list', help='List of Defect System defects to update in SRTool database')
    parser.add_argument('--defect_er', '-e', nargs=1, help='Query list of pending ERs under a project, review them, and assign to Rally themes')
    parser.add_argument('--force', '-f', action='store_true', dest='force_update', help='Force updates')
    parser.add_argument('--verbose', '-v', action='store_true', dest='verbose', help='Verbose debugging')
    parser.add_argument('--user', dest='user', help='User name for Defect System access')
    parser.add_argument('--passwd', dest='passwd', help='User password for Defect System access')

    parser.add_argument('--add', nargs=1, help='Add an existing defect to SRTool defect database')
    parser.add_argument('--delete', nargs=1, help='Delete an existing defect from SRTool defect database')
    parser.add_argument('--new', action='store_const', const='new', dest='command', help='Create a new defect "--new --product-tags --summary --cve --description --reason --priority --components --link"')
    parser.add_argument('--defect-projects', '-j', action='store_const', const='defect-projects', dest='command', help='Defect System projects')


    # Be flexible with arguments to support sub-parse trees
    args, argv = parser.parse_known_args()

    master_log = open("./update_logs/master_log.txt", "a")

    # Authorization
    if args.user:
        srt_user = args.user
    else:
        srt_user = os.environ.get('SRT_USER')
    if args.passwd:
        srt_passwd = args.passwd
    else:
        srt_passwd = os.environ.get('SRT_PASSWD')
    if not srt_user or not srt_passwd:
        msg = "FATAL ERROR: Missing user/password for Defect System access"
        print(msg)
        srt_error_log(msg)
        return 1

    force_update = False
    if None != args.force_update:
        force_update = args.force_update
    if None != args.verbose:
        verbose = True
    if verbose:
        srt_error_log("srtool_defect: NOTE unprocessed arguments: %s" % argv)

    defect_list = ''
    if None != args.defect_update_list:
        defect_list = args.defect_update_list

    if args.defect_er:
        #get_defect_er(args.defect_er[0])
        sys.exit("NOT IMPLEMENTED")
    elif args.add:
        #defect_add_to_defect_db(args.add[0])
        sys.exit("NOT IMPLEMENTED")
    elif args.delete:
        #defect_del_from_defect_db(args.add[0])
        sys.exit("NOT IMPLEMENTED")
    elif 'new' == args.command:
        # Instantiate a sub-parse tree for "new" specific arguments
        # Example:
        # $ ./bin/acme/srtool_defect.py --new --product-tags '{"key":"MERRIE"}' --summary 'User error' \
        #   --cve 'CVE-2019-0000' --description 'Incorrect equipement use' --reason 'beep'  \
        #   --priority High --components "acme" --link 'www.acme.com/cve/CVE-2019-0000'
        new_parser = argparse.ArgumentParser(parents=[parser],add_help=False)
        new_parser.add_argument('--product-tags','-T', dest='tags', help='Product tags for defect tool integration')
        new_parser.add_argument('--summary','-S', help='Defect summary')
        new_parser.add_argument('--cve','-C', help='CVE list')
        new_parser.add_argument('--description','-D', help='Defect summary')
        new_parser.add_argument('--reason','-R', help='Defect reason hint')
        new_parser.add_argument('--priority','-P', help='Defect priority')
        new_parser.add_argument('--components','-O', help='Affected components')
        new_parser.add_argument('--link','-L', help='Link to upstream CVE')
        args, argv = new_parser.parse_known_args()
        if verbose:
            srt_error_log("SRTool_Defect_New: NOTE unprocessed arguments: %s" % argv)
        defect_new_defect(
            args.tags if args.tags else '{}',
            args.summary if args.summary else '',
            args.cve if args.cve else '',
            args.description if args.description else '',
            args.reason if args.reason else '',
            args.priority if args.priority else '',
            args.components if args.components else '',
            args.link if args.link else '',
            )
    elif 'init_defect' == args.command:
        #update_defect(True)
        sys.exit("NOT IMPLEMENTED")
    elif 'update_defect' == args.command:
        #update_defect(False)
        sys.exit("NOT IMPLEMENTED")
    elif defect_list:
        #defect_update_list(defect_list)
        sys.exit("NOT IMPLEMENTED")
    elif 'defect-projects' == args.command:
        #get_projects()
        sys.exit("NOT IMPLEMENTED")
    else:
        sys.exit("Command '%s' not found" % args.command)

if __name__ == '__main__':
    global srtool_basepath

    if verbose: print("srtool_defect(%s)" % sys.argv[1:])

    # fetch any environment overrides
    #set_override('SRTDBG_MINIMAL_DB')

    srtool_basepath = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))))
    main(sys.argv[1:])