Skip to content

Commit 9ba62e5

Browse files
authored
Merge pull request #2 from britive/develop
Develop
2 parents 9b27ebc + 60dc0b6 commit 9ba62e5

62 files changed

Lines changed: 2798 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ __pycache__/
33
*.py[cod]
44
*$py.class
55

6+
.in
7+
.out
8+
.idea*
9+
testing-variables.txt
10+
611
# C extensions
712
*.so
813

README.md

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
~~~

TECH_README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Local Install
2+
3+
~~~
4+
pip install --editable .
5+
~~~
6+
7+
## Build
8+
9+
* Update version in `setup.cfg`
10+
* Push code to GitHub
11+
* Cut a new PR and merge when appropriate
12+
* Run below commands
13+
14+
15+
~~~
16+
python -m pip install --upgrade build
17+
python -m build
18+
~~~
19+
20+
* Cut a new release in GitHub with the version tag
21+
* Add the assets from `dist` directory to the release
22+
23+
## Testing
24+
It is generally advisable to set environment variable `PYBRITIVE_HOME_DIR` to some temp location if you
25+
will be performing the full suite of tests. This will ensure that no changes are made to your existing
26+
configuration or credentials.
27+
28+
Environment variables that should be set for testing include the following.
29+
30+
* `PYBRITIVE_HOME_DIR` - a path to a home directory where `.britive` directory will be created
31+
* `PYBRITIVE_TEST_TENANT` - the tenant name to be used for testing purposes
32+
* `PYBRITIVE_NPM_IMPORT_PROFILE_ALIAS_VALUE` - the IDs of a profile that can be used to test the import process. This should be in format `"appid/envid/profileid/appname"`.
33+
* `PYBRITIVE_ENCRYPTED_CREDENTIAL_PASSPHRASE` - the password for encrypted file credential storage
34+
* `PYBRITIVE_PREPARE_DOT_BRITIVE` - set to true if you want to have the `.britive` directory cleared before starting the tests
35+
* `BRITIVE_API_TOKEN` - set if you want to avoid an interactive login process - the interactive login process will need to be tested separately
36+
37+
Create `./testing-variables.txt` and load what you need so you can easily re-create the needed variables. This file is in `.gitignore`.
38+
39+
The identity used for testing will require access to at least one profile to test `checkout` and `checkin`.
40+
Additionally, the identity will need access to 2 secrets
41+
* one standard secret with path `/pybritive-test-standard` to test `view` - the value of the secret should be generic note with note of `test`
42+
* one file secret with path `/pybritive-test-file` to test `download` - the filename should be `pybritive-test-secret-file.txt` and contain contents of `test`
43+

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[build-system]
2+
requires = [
3+
"setuptools>=42",
4+
"wheel"
5+
]
6+
build-backend = "setuptools.build_meta"

requirements.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
https://github.com/britive/python-sdk/releases/download/v2.7.0/britive-2.7.0.tar.gz
2+
certifi==2022.6.15
3+
charset-normalizer==2.1.0
4+
click==8.1.3
5+
idna==3.3
6+
merge-args==0.1.4
7+
PyYAML==6.0
8+
requests==2.28.1
9+
six==1.16.0
10+
tabulate==0.8.10
11+
toml==0.10.2
12+
urllib3==1.26.9
13+
cryptography==37.0.4
14+
pytest==7.1.2

setup.cfg

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[metadata]
2+
name = pybritive
3+
version = 0.1.0
4+
author = Britive Inc.
5+
author_email = support@britive.com
6+
description = A pure Python CLI for Britive
7+
long_description = file: README.md
8+
long_description_content_type = text/markdown
9+
url = https://www.britive.com
10+
classifiers =
11+
Programming Language :: Python :: 3
12+
License :: OSI Approved :: MIT License
13+
Operating System :: OS Independent
14+
15+
[options]
16+
package_dir =
17+
= src
18+
packages = find:
19+
python_requires = >=3.7
20+
install_requires =
21+
click
22+
requests
23+
PyYAML
24+
merge_args
25+
tabulate
26+
toml
27+
cryptography
28+
britive @ https://github.com/britive/python-sdk/releases/download/v2.7.0/britive-2.7.0.tar.gz#egg=britive-2.7.0
29+
30+
[options.packages.find]
31+
where = src
32+
33+
[options.entry_points]
34+
console_scripts =
35+
pybritive = pybritive.cli_interface:safe_cli

src/pybritive/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)