|
| 1 | +# BETA - NOTE: This is a BETA release. This codebase should not be used for production workloads. |
| 2 | + |
| 3 | +# Britive CLI - Pure Python Implementation |
| 4 | + |
| 5 | + |
| 6 | +## Requirements |
| 7 | + |
| 8 | +* Python 3.7 or higher |
| 9 | +* Active Britive tenant (or nothing is really going to work) |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +`pybritive` will be installed via Python `pip`. The package is not available in PyPi at this time, so it will be |
| 14 | +installed via the published tar balls in the GitHub repo. |
| 15 | + |
| 16 | +~~~bash |
| 17 | +pip install https://github.com/britive/python-cli/releases/download/v0.1.0/pybritive-0.1.0.tar.gz |
| 18 | +~~~ |
| 19 | + |
| 20 | +The end user is free to install the CLI into a virtual environment or in the global scope, so it is available |
| 21 | +everywhere. |
| 22 | + |
| 23 | +## Tenant Configuration |
| 24 | + |
| 25 | +Before `pybritive` can connect to a Britive tenant, it needs to know some details about that tenant. |
| 26 | +This is where `pybritive configure` will help us. |
| 27 | + |
| 28 | +There are 2 ways to tell `pybritive` about tenants. |
| 29 | + |
| 30 | +1. `pybritive configure import`: this will import an existing configuration from the Node.js version of the Britive CLI. |
| 31 | +2. `pybritive configure tenant`: This will prompt (or optionally the values can be passed via flags) for tenant details. |
| 32 | + |
| 33 | +An alias for a tenant can be created in case more than 1 tenant is configured for use. This may be the case for admins |
| 34 | +who may have access to an EA and GA tenant. |
| 35 | + |
| 36 | +## Tenant Selection Logic |
| 37 | + |
| 38 | +There are numerous ways to provide the CLI with the Britive tenant that should be used. The below list is the |
| 39 | +order of operations for determining the tenant. |
| 40 | + |
| 41 | +The tenant excludes `.britive-app.com`. Just include the leftmost part. |
| 42 | +Example: `example.britive-app.com` will have a tenant name in the CLI of `example`. |
| 43 | + |
| 44 | +1. Value retrieved from CLI option/flag `--tenant/-t` |
| 45 | +2. Value retrieved from environment variable `BRITIVE_TENANT` |
| 46 | +3. Value retrieved from `~/.britive/pybritive.config` global variable `default_tenant` |
| 47 | +4. If none of the above are available then check for configured tenants in `~/.britive/pybritive.config` and if there is only 1 tenant configured use it |
| 48 | +5. If all the above fail then error |
| 49 | + |
| 50 | + |
| 51 | +## Credential Selection Logic |
| 52 | + |
| 53 | +There are numerous ways to provide the CLI with the Britive credentials that should be used to authenticate to the |
| 54 | +Britive tenant. The below list is the order of operations for determining the tenant. |
| 55 | + |
| 56 | +1. Value retrieved from CLI option/flag `--token/-T` |
| 57 | +2. Value retrieved from environment variable `BRITIVE_API_TOKEN` |
| 58 | +3. If none of the above are available an interactive login will be performed and temporary credentials will be stored locally for future use with the CLI |
| 59 | + |
| 60 | + |
| 61 | +## Credential Stores |
| 62 | + |
| 63 | +The CLI currently offers two ways in which temporary credentials obtained via interactive login can be stored. |
| 64 | +Future enhancements aim to offer other credential storage options. |
| 65 | + |
| 66 | +### File |
| 67 | + |
| 68 | +Credentials will be stored in a plaintext file located at `~/.britive/pybritive.credentials` |
| 69 | + |
| 70 | +### Encrypted File |
| 71 | +Credentials will be stored in an encrypted file located at `~/.britive/pybritive.credentials.encrypted`. |
| 72 | + |
| 73 | +The user will be prompted for a passphrase to use to encrypt the file. The user can also pass in the passphrase |
| 74 | +via flag `--passphrase/-p` or via environment variable `PYBRITIVE_ENCRYPTED_CREDENTIAL_PASSPHRASE`. |
| 75 | + |
| 76 | + |
| 77 | +## Home Directory |
| 78 | +By default, files that `pybritive` requires will be persisted to `~/.britive/`. |
| 79 | + |
| 80 | +This can be overwritten by specifying environment variable `PYBRITIVE_HOME_DIR`. This should be a path to where |
| 81 | +the end user wants to persist the `.britive` directory. Note that `.britive` will still be created so do not specify |
| 82 | +that as part of the path. |
| 83 | + |
| 84 | +## Shell Completion |
| 85 | +Behind the scenes the `pybritive` CLI tool uses the python `click` package. `click` offers shell completion for |
| 86 | +the following shells. |
| 87 | + |
| 88 | +* Bash |
| 89 | +* Zsh |
| 90 | +* Fish |
| 91 | +* PowerShell (work in progress for the `pybritive` cli - NOT WORKING YET) |
| 92 | + |
| 93 | +In order to set up shell completion, follow these steps. Once complete either `source` your environment again |
| 94 | +or start a new shell in order for the changes to be loaded. |
| 95 | + |
| 96 | +### Bash |
| 97 | +Save the completion script somewhere. |
| 98 | + |
| 99 | +~~~bash |
| 100 | +_PYBRITIVE_COMPLETE=bash_source pybritive > ~/.pybritive-complete.bash |
| 101 | +~~~ |
| 102 | + |
| 103 | +Source the file in `~/.bashrc`. |
| 104 | + |
| 105 | +~~~ |
| 106 | +source ~/.pybritive-complete.bash |
| 107 | +~~~ |
| 108 | + |
| 109 | +### Zsh |
| 110 | +Save the completion script somewhere. |
| 111 | + |
| 112 | +~~~bash |
| 113 | +_PYBRITIVE_COMPLETE=zsh_source pybritive > ~/.pybritive-complete.zsh |
| 114 | +~~~ |
| 115 | + |
| 116 | +Source the file in `~/.zshrc`. |
| 117 | + |
| 118 | +~~~ |
| 119 | +source ~/.pybritive-complete.zsh |
| 120 | +~~~ |
| 121 | + |
| 122 | +### Fish |
| 123 | +Save the completion script to the `fish` completions directory. |
| 124 | + |
| 125 | +~~~bash |
| 126 | +_PYBRITIVE_COMPLETE=fish_source pybritive > ~/.config/fish/completions/foo-bar.fish |
| 127 | +~~~ |
| 128 | + |
| 129 | +### PowerShell |
| 130 | +Append the below code to your PowerShell profile. |
| 131 | + |
| 132 | +~~~ |
| 133 | +if ((Test-Path Function:\TabExpansion) -and -not (Test-Path Function:\pybritiveTabExpansionBackup)) { |
| 134 | + Rename-Item Function:\TabExpansion pybritiveTabExpansionBackup |
| 135 | +} |
| 136 | +
|
| 137 | +function TabExpansion($line, $lastWord) { |
| 138 | + $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart() |
| 139 | + $aliases = @("pybritive") + @(Get-Alias | where { $_.Definition -eq "pybritive" } | select -Exp Name) |
| 140 | + $aliasPattern = "($($aliases -join '|'))" |
| 141 | + if($lastBlock -match "^$aliasPattern ") { |
| 142 | + $Env:_PYBRITIVE_COMPLETE = "complete-powershell" |
| 143 | + $Env:COMMANDLINE = "$lastBlock" |
| 144 | + (pybritive) | ? {$_.trim() -ne "" } |
| 145 | + Remove-Item Env:_PYBRITIVE_COMPLETE |
| 146 | + Remove-Item Env:COMMANDLINE |
| 147 | + } |
| 148 | + elseif (Test-Path Function:\pybritiveTabExpansionBackup) { |
| 149 | + # Fall back on existing tab expansion |
| 150 | + pybritiveTabExpansionBackup $line $lastWord |
| 151 | + } |
| 152 | +} |
| 153 | +~~~ |
| 154 | + |
| 155 | + |
| 156 | +The location of your PowerShell profile can be found with command |
| 157 | + |
| 158 | +~~~bash |
| 159 | +echo $profile |
| 160 | +~~~ |
| 161 | + |
| 162 | +And is generally something like `C:\Users\{user}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1`. |
| 163 | + |
| 164 | +### Shell Completion - Profiles - Local Cache |
| 165 | + |
| 166 | +By default, shell completion only completes commands and options/flags as those values are available without |
| 167 | +any authentication to a Britive tenant. |
| 168 | + |
| 169 | +There is an option to enable shell completion for profiles and profile aliases for use with `checkout` and `checkin`. |
| 170 | + |
| 171 | +In order enable this, run the following command. |
| 172 | + |
| 173 | +~~~bash |
| 174 | +pybritive cache profiles |
| 175 | +~~~ |
| 176 | + |
| 177 | +This will locally cache profiles for which the authenticated user has permissions. If multiple tenants are being used |
| 178 | +then each tenant will need to be cached individually. All profiles across all tenants will be available during shell |
| 179 | +completion. |
| 180 | + |
| 181 | +The cache will not be updated over time. In order to update the cache more regularly run the following command. |
| 182 | +Note that this config flag is NOT available directly via `pybrtitve configure global ...`. |
| 183 | + |
| 184 | +~~~bash |
| 185 | +pybritive configure update global auto-refresh-profile-cache true |
| 186 | +~~~ |
| 187 | + |
| 188 | +To turn the feature off run |
| 189 | + |
| 190 | +~~~bash |
| 191 | +pybritive configure update global auto-refresh-profile-cache false |
| 192 | +pybritive cache clear |
| 193 | +~~~ |
0 commit comments