Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / lib / loas.py
1 # Copyright 2014 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Manage Google Low Overhead Authentication Service (LOAS) tasks.
6
7 This is used by scripts that run outside of the chroot and require access to
8 Google production resources.
9
10 If you don't know what any of this means, then you don't need this module :).
11 """
12
13 from __future__ import print_function
14
15 import datetime
16 import logging
17 import re
18 import socket
19
20 from chromite.lib import alerts
21 from chromite.lib import cros_build_lib
22
23
24 class LoasError(Exception):
25   """Raised when a LOAS error occurs"""
26
27
28 class Loas(object):
29   """Class for holding all the various LOAS cruft."""
30
31   def __init__(self, user, email_notify):
32     self.user = user
33     self.email_notify = email_notify
34     self.enroll_msg = 'become -t -c "prodaccess --sslenroll" %s@%s' % (
35         self.user, socket.getfqdn())
36     self.last_notification = (
37         datetime.date.today() - datetime.timedelta(weeks=10))
38
39   def Check(self):
40     logging.debug('Checking LOAS credentials for %s', self.user)
41     cmd = ['runloas', '/usr/bin/loas_check']
42
43     # Error message to print when loas credential check fails. This usually
44     # is the result of production credentials expiring for accessing
45     # Keystore for the unwrapping private key.
46     loas_error = 'loas_check for %s failed! Did you run: %s' % (
47         self.user, self.enroll_msg)
48     try:
49       cros_build_lib.SudoRunCommand(cmd,
50                                     user=self.user,
51                                     error_message=loas_error)
52     except cros_build_lib.RunCommandError as e:
53       raise LoasError(e.msg)
54
55   def Status(self):
56     # Only bother checking once a day.  Our certs are valid in the
57     # range of weeks, so there's no need to constantly do this.
58     if (datetime.date.today() < self.last_notification +
59                                 datetime.timedelta(days=1)):
60       return
61
62     cmd = ['prodcertstatus', '--check_loas_cert_location', 'sslenrolled']
63     result = cros_build_lib.SudoRunCommand(cmd,
64                                            user=self.user,
65                                            error_code_ok=True,
66                                            redirect_stdout=True)
67
68     # Figure out how many days are left.  The command should display:
69     # SSL-ENROLLED CERT cert expires in about 22 days
70     m = re.search(r'cert expires in about ([0-9]+) days', result.output)
71     if m:
72       days_left = int(m.group(1))
73     else:
74       days_left = 0
75
76     # Send out one notification a day if there's a week or less left
77     # before our creds expire.
78     if days_left <= 7:
79       alerts.SendEmail(
80           'Loas certs expiring soon!',
81           self.email_notify,
82           message='Please run:\n %s\n\n%s\n%s' %
83               (self.enroll_msg, result.output, result.error))
84       self.last_notification = datetime.date.today()
85     else:
86       # We won't expire for a while, so stop the periodic polling.
87       self.last_notification = (
88           datetime.date.today() + datetime.timedelta(days=days_left - 8))