k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/gubernator/main.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2016 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  import os
    18  import yaml
    19  import webapp2
    20  
    21  from google.appengine.api import users
    22  
    23  import github_auth
    24  import view_base
    25  import view_build
    26  import view_logs
    27  import view_pr
    28  import secrets
    29  
    30  
    31  hostname = secrets.get_hostname()
    32  
    33  
    34  def get_app_config():
    35      with open('config.yaml') as config_file:
    36          return yaml.safe_load(config_file)
    37  
    38  config = {
    39      'webapp2_extras.sessions': {
    40          'secret_key': None,  # filled in on the first request
    41          'cookie_args': {
    42              # we don't have SSL For local development
    43              'secure': os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'),
    44              'httponly': True,
    45          },
    46      },
    47      'github_client': None,  # filled in the first time auth is needed
    48  }
    49  
    50  config.update(get_app_config())
    51  
    52  class Warmup(webapp2.RequestHandler):
    53      """Warms up gubernator."""
    54      def get(self):
    55          """Receives the warmup request."""
    56          try:
    57              self.app.config['github_client'] = secrets.get('github_client')
    58          except KeyError:
    59              pass  # dev server, generally
    60  
    61          self.response.headers['Content-Type'] = 'text/plain'
    62          self.response.write('Warmup successful')
    63  
    64  
    65  class ConfigHandler(view_base.BaseHandler):
    66      """Handles admin config of gubernator."""
    67      def get(self):
    68          self.render('config.html', {'hostname': hostname})
    69  
    70      def post(self):
    71          self.check_csrf()
    72          if users.is_current_user_admin():
    73              oauth_set = False
    74              webhook_set = False
    75              token_set = False
    76  
    77              github_id = self.request.get('github_id')
    78              github_secret = self.request.get('github_secret')
    79              github_token = self.request.get('github_token')
    80              github_client_key = 'github_client'
    81              if self.request.get('github_client_host'):
    82                  # enable custom domains pointed at the same app to have their
    83                  # own github oauth config.
    84                  github_client_key = 'github_client_%s' % \
    85                      self.request.get('github_client_host')
    86              if github_id and github_secret:
    87                  value = {'id': github_id, 'secret': github_secret}
    88                  secrets.put(github_client_key, value)
    89                  app.config[github_client_key] = value
    90                  oauth_set = True
    91              github_webhook_secret = self.request.get('github_webhook_secret')
    92              if github_webhook_secret:
    93                  secrets.put('github_webhook_secret',
    94                              github_webhook_secret,
    95                              per_host=False)
    96                  webhook_set = True
    97              if github_token:
    98                  secrets.put('github_token', github_token, per_host=False)
    99                  token_set = True
   100              self.render('config.html',
   101                          dict(hostname=hostname,
   102                               oauth_set=oauth_set,
   103                               webhook_set=webhook_set,
   104                               token_set=token_set))
   105          else:
   106              self.abort(403)
   107  
   108  
   109  app = webapp2.WSGIApplication([
   110      ('/_ah/warmup', Warmup),
   111      (r'/', view_base.IndexHandler),
   112      (r'/jobs/(.*)$', view_build.JobListHandler),
   113      (r'/builds/(.*)/([^/]+)/?', view_build.BuildListHandler),
   114      (r'/build/(.*)/([^/]+)/([-\da-f_:.]+)/?', view_build.BuildHandler),
   115      (r'/build/(.*)/([^/]+)/([-\da-f_:.]+)/nodelog*', view_logs.NodeLogHandler),
   116      (r'/pr((?:/[^/]+){0,2})/(\d+|batch)', view_pr.PRHandler),
   117      (r'/pr/?', view_pr.PRDashboard),
   118      (r'/pr/([-\w]+)', view_pr.PRDashboard),
   119      (r'/pr/(.*/build-log.txt)', view_pr.PRBuildLogHandler),
   120      (r'/github_auth(.*)', github_auth.Endpoint),
   121      (r'/config', ConfigHandler),
   122      (r'/gcsproxy', view_build.GcsProxyHandler)
   123  ], debug=True, config=config)