github.com/abayer/test-infra@v0.0.5/gubernator/secrets.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2018 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  
    19  from google.appengine.ext import ndb
    20  
    21  from google.appengine.api import modules
    22  from google.appengine.api import app_identity
    23  
    24  def get_hostname():
    25      hostname = app_identity.get_default_version_hostname()
    26      if 'testbed' not in os.environ.get('SERVER_SOFTWARE', 'testbed'):
    27          current_version = modules.modules.get_current_version_name()
    28          if current_version and modules.modules.get_default_version() != current_version:
    29              hostname = '%s-dot-%s' % (current_version, hostname)
    30      return hostname
    31  
    32  class Secret(ndb.Model):
    33      value = ndb.JsonProperty()
    34  
    35      @staticmethod
    36      def make_key(key, per_host):
    37          if per_host:
    38              return ndb.Key(Secret, '%s\t%s' % (get_hostname(), key))
    39          return ndb.Key(Secret, key)
    40  
    41      @staticmethod
    42      def make(key, value, per_host):
    43          return Secret(key=Secret.make_key(key, per_host), value=value)
    44  
    45  
    46  def get(key, per_host=True):
    47      data = Secret.make_key(key, per_host).get()
    48      if not data:
    49          raise KeyError
    50      return data.value
    51  
    52  
    53  def put(key, value, per_host=True):
    54      Secret.make(key, value, per_host).put()