github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/cmd/grandmatriarch/bake.sh (about)

     1  #!/bin/bash
     2  # Copyright 2018 The Kubernetes Authors.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  # TODO(fejta): make this a good program, not bash
    17  
    18  set -o errexit
    19  set -o nounset
    20  set -o pipefail
    21  
    22  if [[ "$#" != 2 ]]; then
    23    echo "Usage: $(basename "$0") <json creds> <name>" >&2
    24    exit 1
    25  fi
    26  
    27  creds="$1"
    28  name="$2"
    29  
    30  if [[ ! -f "$creds" ]]; then
    31    echo "Not found: $creds" >&2
    32    exit 1
    33  fi
    34  gcloud auth activate-service-account --key-file="$creds"
    35  gcloud auth list
    36  
    37  user="$(grep -o -E '[^"]+@[^"]+' "$creds")"
    38  create=yes
    39  
    40  print-token() {
    41    gcloud config config-helper --force-auth-refresh | grep access_token | grep -o -E '[^ ]+$'
    42  }
    43  
    44  # Format of the cookiefile is:
    45  # * one line per cookie
    46  # * tab separate the following fields:
    47  #   - DOMAIN
    48  #   - INITIAL_DOT
    49  #   - PATH
    50  #   - PATH_SPECIFIED
    51  #   - expires
    52  #   - name
    53  #   - value
    54  
    55  print-cookie() {
    56    if [[ "$#" != 4 ]]; then
    57      echo "Usage: print-cookie <HOST> <IS_DOT> <EXPIRES_EPOCH> <TOKEN>" >&2
    58      return 1
    59    fi
    60    host="$1"
    61    dot="$2"
    62    exp="$3"
    63    tok="$4"
    64    for part in "$host" "$dot" / TRUE "$exp" o; do
    65      echo -n ${part}$'\t' # apparently $'\t' is tab
    66    done
    67    echo "$tok"
    68  }
    69  
    70  
    71  while true; do
    72    token=$(print-token)
    73    expire=$(expr 60 \* 60 + $(date +%s))
    74    echo "token expires at"
    75    date -d "@$expire"
    76    print-cookie .googlesource.com TRUE "$expire" "$token" > cookies
    77    print-cookie source.developers.google.com FALSE "$expire" "$token" >> cookies
    78    md5sum cookies
    79  
    80    kubectl create secret generic "$name" --from-file=cookies --dry-run -o yaml > secret.yaml
    81    if ! kubectl get -f secret.yaml; then
    82      verb=create
    83    else
    84      verb=replace
    85    fi
    86    kubectl "$verb" -f secret.yaml
    87    echo "successfully updated token, sleeping for 20m..."
    88    sleep 20m
    89  done