~ubuntu-branches/ubuntu/trusty/nordugrid-arc/trusty-proposed

« back to all changes in this revision

Viewing changes to src/services/a-rex/jura/ssm/ssm2.py

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2013-06-14 16:42:17 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20130614164217-nmuy3qabkkjkdd6v
Tags: 3.0.2-1
* 3.0.2 Release
* Drop deprecated find syntax (Closes: #711708)

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
    CONNECTION_TIMEOUT = 10
53
53
    
54
54
    def __init__(self, hosts_and_ports, qpath, cert, key, dest=None, listen=None, 
55
 
                 capath=None, use_ssl=False, username=None, password=None, enc_cert=None,
56
 
                 pidfile=None):
 
55
                 capath=None, check_crls=False, use_ssl=False, username=None, password=None, 
 
56
                 enc_cert=None, verify_enc_cert=True, pidfile=None):
57
57
        '''
58
58
        Creates an SSM2 object.  If a listen value is supplied,
59
 
        this SSM2 will be a consumer.
 
59
        this SSM2 will be a receiver.
60
60
        '''
61
61
        self._conn = None
62
62
        self._last_msg = None
66
66
        self._key = key
67
67
        self._enc_cert = enc_cert
68
68
        self._capath = capath
 
69
        self._check_crls = check_crls
69
70
        self._user = username
70
71
        self._pwd = password
71
72
        self._use_ssl = use_ssl
72
 
        # Use pwd if we're supplied user and pwd
 
73
        # use pwd auth if we're supplied both user and pwd
73
74
        self._use_pwd = username is not None and password is not None
74
75
        self.connected = False
75
76
        
77
78
        self._dest = dest
78
79
        
79
80
        self._valid_dns = []
 
81
        self._pidfile = pidfile
80
82
        
 
83
        # create the filesystem queues for accepted and rejected messages
81
84
        if dest is not None and listen is None:
82
85
            self._outq = QueueSimple(qpath)
83
86
        elif listen is not None:
87
90
            self._rejectq = Queue(rejectqpath, schema=Ssm2.REJECT_SCHEMA)
88
91
        else:
89
92
            raise Ssm2Exception('SSM must be either producer or consumer.')
90
 
        
 
93
        # check that the cert and key match
91
94
        if not crypto.check_cert_key(self._cert, self._key):
92
95
            raise Ssm2Exception('Cert and key don\'t match.')
93
 
        
 
96
        # check the server certificate provided
94
97
        if enc_cert is not None:
95
 
            if not os.path.isfile(enc_cert):
96
 
                raise Ssm2Exception('Specified certificate file does not exist.')
97
 
        
98
 
        self._pidfile = pidfile
99
 
        
100
 
        
 
98
            log.info('Messages will be encrypted using %s' % enc_cert)
 
99
            if not os.path.isfile(self._enc_cert):
 
100
                raise Ssm2Exception('Specified certificate file does not exist: %s.' % self._enc_cert)
 
101
            if verify_enc_cert:
 
102
                if not crypto.verify_cert_path(self._enc_cert, self._capath, self._check_crls):
 
103
                    raise Ssm2Exception('Failed to verify server certificate %s against CA path %s.' 
 
104
                                         % (self._enc_cert, self._capath))
 
105
            
101
106
    
102
107
    def set_dns(self, dn_list):
103
108
        '''
133
138
        raw_msg, signer = self._handle_msg(body)
134
139
        
135
140
        try:
136
 
            if raw_msg is None:
137
 
                log.warn('Could not extract message; rejecting.')
138
 
                if signer is None:
 
141
            if raw_msg is None: # the message has been rejected
 
142
                log.warn('Message rejected.')
 
143
                if signer is None: # crypto failed
 
144
                    err_msg = 'Could not extract message.'
 
145
                    log.warn(err_msg)
139
146
                    signer = 'Not available.'
 
147
                else: # crypto ok but signer not verified
 
148
                    err_msg = 'Signer not in valid DNs list.'
 
149
                    log.warn(err_msg)
 
150
                    
140
151
                self._rejectq.add({'body': body,
141
152
                                   'signer': signer,
142
153
                                   'empaid': empaid,
143
 
                                   'error': 'Could not extract message.'})
144
 
            else:
 
154
                                   'error': err_msg})
 
155
            else: # message verified ok
145
156
                self._inq.add({'body': raw_msg, 
146
157
                               'signer':signer, 
147
158
                               'empaid': headers['empa-id']})
204
215
        
205
216
        # always signed
206
217
        try:
207
 
            message, signer = crypto.verify(text, self._capath, False)
 
218
            message, signer = crypto.verify(text, self._capath, self._check_crls)
208
219
        except crypto.CryptoException, e:
209
220
            log.error('Failed to verify message: %s' % e)
210
221
            return None, None
211
222
        
212
223
        if signer not in self._valid_dns:
213
 
            log.error('Received message from invalid signer: %s' % signer)
 
224
            log.error('Message signer not in the valid DNs list: %s' % signer)
214
225
            return None, signer
215
226
        else:
216
227
            log.info('Valid signer: %s' % signer)
232
243
            if self._enc_cert is not None:
233
244
                to_send = crypto.encrypt(to_send, self._enc_cert)
234
245
        else:
235
 
            to_send = None
 
246
            to_send = ''
236
247
            
237
248
        self._conn.send(to_send, headers=headers)
238
249
        
282
293
        Create the self._connection object with the appropriate properties,
283
294
        but don't try to start the connection.
284
295
        '''
 
296
        if self._use_ssl:
 
297
            log.info('Connecting using SSL...')
 
298
            
285
299
        self._conn = stomp.Connection([(host, port)], 
286
300
                                      use_ssl=self._use_ssl,
287
301
                                      user = self._user,
339
353
            
340
354
        # If reconnection fails, admit defeat.
341
355
        if not self.connected:
342
 
            error_message = 'Reconnection attempts failed and have been abandoned.'
343
 
            raise Ssm2Exception(error_message)
 
356
            err_msg = 'Reconnection attempts failed and have been abandoned.'
 
357
            raise Ssm2Exception(err_msg)
344
358
        
345
359
    def start_connection(self):
346
360
        '''