import re


def _CanonicalizePath(secret_path):
  """Canonicalizes secret path to the form `/mount_path:/secret_file_path`.

  Gcloud secret path is more restrictive than the backend (shortn/_bwgb3xdRxL).
  Paths are reduced to their canonical forms before the request is made.

  Args:
    secret_path: Complete path to the secret.

  Returns:
    Canonicalized secret path.
  """
  secret_path = re.sub(r'/+', '/', secret_path)
  mount_path, _, secret_file_path = secret_path.rpartition('/')
  if ':' in secret_path:
    mount_path, _, secret_file_path = secret_path.partition(':')
  mount_path = mount_path[:-1] if mount_path.endswith('/') else mount_path
  secret_file_path = '/' + secret_file_path if not secret_file_path.startswith(
      '/') else secret_file_path
  return mount_path + ':' + secret_file_path