Skip to content

Commit 099f664

Browse files
committed
Use pydantic for validations
Remove redundancies in arguments Update README.md
1 parent 81dc4ed commit 099f664

20 files changed

+332
-379
lines changed

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ venv
1414
# Sphinx
1515
_build
1616

17-
# Test script
18-
test.py
19-
2017
# Build stuff
2118
*.egg-info
2219
dist

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
fail_fast: true
12
repos:
23
-
34
repo: https://github.com/PyCQA/flake8

README.md

+33-28
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,16 @@ Expose an app/api running on local host to public internet using AWS EC2
1111
Environment variables can be loaded from a `.env` file.
1212

1313
**Mandatory Arg:**
14-
- `PORT`: Port number that has to be exposed (on which a localhost `service/app/api` is running)
14+
- **PORT**: Port number that has to be exposed (on which a localhost `service/app/api` is running)
1515

1616
**Optional Args:**
17-
- `IMAGE_ID`: ID of any public AMI with an Ubuntu OS. Defaults to a region specific image ID.
18-
- `AWS_ACCESS_KEY`: Access key to access AWS resources. Defaults to `~/.aws/config`
19-
- `AWS_SECRET_KEY`: Secret key to access AWS resources. Defaults to `~/.aws/config`
20-
- `AWS_REGION_NAME`: Region name where the instance should live. Defaults to `US-WEST-2`
21-
22-
*Optionally `.env` files can also be scanned for:*
23-
```python
24-
import expose
25-
26-
expose.load_env(scan=True)
27-
```
17+
- **IMAGE_ID**: ID of any public AMI with an Ubuntu OS. Defaults to a region specific image ID.
18+
- **AWS_ACCESS_KEY**: Access key to access AWS resources. Defaults to `~/.aws/config`
19+
- **AWS_SECRET_KEY**: Secret key to access AWS resources. Defaults to `~/.aws/config`
20+
- **AWS_REGION_NAME**: Region name where the instance should live. Defaults to `US-WEST-2`
21+
- **KEY_PAIR**: Name for the ec2 key pair file. Defaults to `expose_localhost`
22+
- **SECURITY_GROUP**: Name for the security group to allow port access. Defaults to `Expose Localhost`
23+
- **SERVER_INFO**: Name for the JSON file to store the configuration info. Defaults to `server_info.json`
2824

2925
<details>
3026
<summary><strong>Setup a custom endpoint</strong></summary>
@@ -33,23 +29,21 @@ The public DNS names for EC2 instances are long and messy. To avoid that, an `A`
3329

3430
:warning: &nbsp; Requires an active hosted zone on `route53`.
3531

36-
- `DOMAIN`: If the domain name is registered using `route53`. *Example: `mywebsite.com`*
37-
- `SUBDOMAIN`: Sub-domain that has to be added for the domain name. *Example: `tunnel.mywebsite.com`*
32+
- **DOMAIN**: Domain name registered using `route53`. *Example: `mywebsite.com`*
33+
- **SUBDOMAIN**: Sub-domain that has to be added for the domain name. *Example: `tunnel`*
3834

39-
&nbsp; &nbsp; &nbsp; &nbsp; :bulb: &nbsp; This will be the endpoint to access the localhost.
35+
&nbsp; &nbsp; &nbsp; &nbsp; :bulb: &nbsp; `tunnel.mywebsite.com` will be the endpoint to access the localhost from public internet.
4036

4137
</details>
4238

4339
#### Certificate:
4440
- Securing the tunnel requires the certificate chain and the key file.
45-
- These two files should be saved as `cert.pem` and `key.pem` in either `~.ssh/*.pem` or within `expose` repository.
46-
- No certs? No problem. [`expose`](https://github.com/thevickypedia/expose/blob/main/expose/helpers/cert.py) will
47-
generate a self-signed certificate and a private key automatically.
41+
- The certificate and key files should be in `pem` format stored within `expose` directory.
42+
- File names should be stored as `key_file` and `cert_file` env var.
43+
- No certs? No problem. `expose` will generate a self-signed certificate and a private key automatically.
4844

4945
<details>
50-
<summary><strong>Generate private SSL certificate</strong></summary>
51-
52-
Unfortunately not many SSL certificate providers give the liberty to download key files. But `expose`, can use private certificates.
46+
<summary><strong>Generate self-signed SSL certificate</strong></summary>
5347

5448
:warning: &nbsp; Some web browsers might throw a warning and some might even block a self-signed certificate/private CA.
5549

@@ -61,8 +55,8 @@ To manually generate a self-signed cert:
6155

6256
Simply let `expose` create a self-signed SSL certificate and a private key.
6357

64-
- `EMAIL_ADDRESS`: Email address to create the self-signed SSL and private key. Defaults to `[email protected]`
65-
- `ORGANIZATION`: Organization name for the certificate. Defaults to the AWS endpoint.
58+
- **EMAIL_ADDRESS**: Email address to create the self-signed SSL and private key. Defaults to `[email protected]`
59+
- **ORGANIZATION**: Organization name for the certificate. Defaults to the AWS endpoint.
6660

6761
</details>
6862

@@ -74,13 +68,24 @@ python3 -m pip install expose-localhost
7468

7569
###### Tunneling:
7670
```python
71+
import os
72+
73+
os.environ['env_file'] = 'custom' # to load a custom .env file
74+
7775
import expose
7876

77+
# Instantiate object
7978
tunnel = expose.Tunnel()
80-
# set 'purge' flag to 'True' to delete AWS resources if configuration fails
79+
80+
# Start tunneling
8181
tunnel.start()
82+
83+
# set 'purge' flag to 'True' to delete AWS resources if configuration fails
84+
# tunnel.start(purge=True)
85+
8286
# sleep or do something else
83-
# set 'partial' flag to 'True' to skip SG deletion
87+
88+
# Stop tunneling - deletes all AWS resources acquired
8489
tunnel.stop()
8590
```
8691

@@ -101,20 +106,20 @@ Clean code with pre-commit hooks: [`flake8`](https://flake8.pycqa.org/en/latest/
101106
## [Release Notes](https://github.com/thevickypedia/expose/blob/main/release_notes.rst)
102107
**Requirement**
103108
```shell
104-
python -m pip install changelog-generator
109+
python -m pip install gitverse
105110
```
106111

107112
**Usage**
108113
```shell
109-
changelog reverse -f release_notes.rst -t 'Release Notes'
114+
gitverse-release reverse -f release_notes.rst -t 'Release Notes'
110115
```
111116

112117
## Linting
113118
`PreCommit` will ensure linting, and the doc creation are run on every commit.
114119

115120
**Requirement**
116121
```shell
117-
pip install sphinx==5.1.1 pre-commit recommonmark gitverse
122+
pip install sphinx==5.1.1 pre-commit recommonmark
118123
```
119124

120125
**Usage**

docs/README.html

+34-27
Original file line numberDiff line numberDiff line change
@@ -60,50 +60,46 @@ <h3>Environment Variables:<a class="headerlink" href="#environment-variables" ti
6060
<p>Environment variables can be loaded from a <code class="docutils literal notranslate"><span class="pre">.env</span></code> file.</p>
6161
<p><strong>Mandatory Arg:</strong></p>
6262
<ul class="simple">
63-
<li><p><code class="docutils literal notranslate"><span class="pre">PORT</span></code>: Port number that has to be exposed (on which a localhost <code class="docutils literal notranslate"><span class="pre">service/app/api</span></code> is running)</p></li>
63+
<li><p><strong>PORT</strong>: Port number that has to be exposed (on which a localhost <code class="docutils literal notranslate"><span class="pre">service/app/api</span></code> is running)</p></li>
6464
</ul>
6565
<p><strong>Optional Args:</strong></p>
6666
<ul class="simple">
67-
<li><p><code class="docutils literal notranslate"><span class="pre">IMAGE_ID</span></code>: ID of any public AMI with an Ubuntu OS. Defaults to a region specific image ID.</p></li>
68-
<li><p><code class="docutils literal notranslate"><span class="pre">AWS_ACCESS_KEY</span></code>: Access key to access AWS resources. Defaults to <code class="docutils literal notranslate"><span class="pre">~/.aws/config</span></code></p></li>
69-
<li><p><code class="docutils literal notranslate"><span class="pre">AWS_SECRET_KEY</span></code>: Secret key to access AWS resources. Defaults to <code class="docutils literal notranslate"><span class="pre">~/.aws/config</span></code></p></li>
70-
<li><p><code class="docutils literal notranslate"><span class="pre">AWS_REGION_NAME</span></code>: Region name where the instance should live. Defaults to <code class="docutils literal notranslate"><span class="pre">US-WEST-2</span></code></p></li>
67+
<li><p><strong>IMAGE_ID</strong>: ID of any public AMI with an Ubuntu OS. Defaults to a region specific image ID.</p></li>
68+
<li><p><strong>AWS_ACCESS_KEY</strong>: Access key to access AWS resources. Defaults to <code class="docutils literal notranslate"><span class="pre">~/.aws/config</span></code></p></li>
69+
<li><p><strong>AWS_SECRET_KEY</strong>: Secret key to access AWS resources. Defaults to <code class="docutils literal notranslate"><span class="pre">~/.aws/config</span></code></p></li>
70+
<li><p><strong>AWS_REGION_NAME</strong>: Region name where the instance should live. Defaults to <code class="docutils literal notranslate"><span class="pre">US-WEST-2</span></code></p></li>
71+
<li><p><strong>KEY_PAIR</strong>: Name for the ec2 key pair file. Defaults to <code class="docutils literal notranslate"><span class="pre">expose_localhost</span></code></p></li>
72+
<li><p><strong>SECURITY_GROUP</strong>: Name for the security group to allow port access. Defaults to <code class="docutils literal notranslate"><span class="pre">Expose</span> <span class="pre">Localhost</span></code></p></li>
73+
<li><p><strong>SERVER_INFO</strong>: Name for the JSON file to store the configuration info. Defaults to <code class="docutils literal notranslate"><span class="pre">server_info.json</span></code></p></li>
7174
</ul>
72-
<p><em>Optionally <code class="docutils literal notranslate"><span class="pre">.env</span></code> files can also be scanned for:</em></p>
73-
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">expose</span>
74-
75-
<span class="n">expose</span><span class="o">.</span><span class="n">load_env</span><span class="p">(</span><span class="n">scan</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
76-
</pre></div>
77-
</div>
7875
<details>
7976
<summary><strong>Setup a custom endpoint</strong></summary><p>The public DNS names for EC2 instances are long and messy. To avoid that, an <code class="docutils literal notranslate"><span class="pre">A</span></code> record can be added to the <code class="docutils literal notranslate"><span class="pre">route53</span></code> hosted zone.</p>
8077
<p>:warning:   Requires an active hosted zone on <code class="docutils literal notranslate"><span class="pre">route53</span></code>.</p>
8178
<ul class="simple">
82-
<li><p><code class="docutils literal notranslate"><span class="pre">DOMAIN</span></code>: If the domain name is registered using <code class="docutils literal notranslate"><span class="pre">route53</span></code>. <em>Example: <code class="docutils literal notranslate"><span class="pre">mywebsite.com</span></code></em></p></li>
83-
<li><p><code class="docutils literal notranslate"><span class="pre">SUBDOMAIN</span></code>: Sub-domain that has to be added for the domain name. <em>Example: <code class="docutils literal notranslate"><span class="pre">tunnel.mywebsite.com</span></code></em></p></li>
79+
<li><p><strong>DOMAIN</strong>: Domain name registered using <code class="docutils literal notranslate"><span class="pre">route53</span></code>. <em>Example: <code class="docutils literal notranslate"><span class="pre">mywebsite.com</span></code></em></p></li>
80+
<li><p><strong>SUBDOMAIN</strong>: Sub-domain that has to be added for the domain name. <em>Example: <code class="docutils literal notranslate"><span class="pre">tunnel</span></code></em></p></li>
8481
</ul>
85-
<p>        :bulb:   This will be the endpoint to access the localhost.</p>
82+
<p>        :bulb:   <code class="docutils literal notranslate"><span class="pre">tunnel.mywebsite.com</span></code> will be the endpoint to access the localhost from public internet.</p>
8683
</details></section>
8784
<section id="certificate">
8885
<h3>Certificate:<a class="headerlink" href="#certificate" title="Permalink to this heading"></a></h3>
8986
<ul class="simple">
9087
<li><p>Securing the tunnel requires the certificate chain and the key file.</p></li>
91-
<li><p>These two files should be saved as <code class="docutils literal notranslate"><span class="pre">cert.pem</span></code> and <code class="docutils literal notranslate"><span class="pre">key.pem</span></code> in either <code class="docutils literal notranslate"><span class="pre">~.ssh/*.pem</span></code> or within <code class="docutils literal notranslate"><span class="pre">expose</span></code> repository.</p></li>
92-
<li><p>No certs? No problem. <a class="reference external" href="https://github.com/thevickypedia/expose/blob/main/expose/helpers/cert.py"><code class="docutils literal notranslate"><span class="pre">expose</span></code></a> will
93-
generate a self-signed certificate and a private key automatically.</p></li>
88+
<li><p>The certificate and key files should be in <code class="docutils literal notranslate"><span class="pre">pem</span></code> format stored within <code class="docutils literal notranslate"><span class="pre">expose</span></code> directory.</p></li>
89+
<li><p>File names should be stored as <code class="docutils literal notranslate"><span class="pre">key_file</span></code> and <code class="docutils literal notranslate"><span class="pre">cert_file</span></code> env var.</p></li>
90+
<li><p>No certs? No problem. <code class="docutils literal notranslate"><span class="pre">expose</span></code> will generate a self-signed certificate and a private key automatically.</p></li>
9491
</ul>
9592
<details>
96-
<summary><strong>Generate private SSL certificate</strong></summary><p>Unfortunately not many SSL certificate providers give the liberty to download key files. But <code class="docutils literal notranslate"><span class="pre">expose</span></code>, can use private certificates.</p>
97-
<p>:warning:   Some web browsers might throw a warning and some might even block a self-signed certificate/private CA.</p>
93+
<summary><strong>Generate self-signed SSL certificate</strong></summary><p>:warning:   Some web browsers might throw a warning and some might even block a self-signed certificate/private CA.</p>
9894
<p>To manually generate a self-signed cert:</p>
9995
<blockquote>
10096
<div><p><code class="docutils literal notranslate"><span class="pre">openssl</span> <span class="pre">req</span> <span class="pre">-newkey</span> <span class="pre">rsa:2048</span> <span class="pre">-sha256</span> <span class="pre">-nodes</span> <span class="pre">-keyout</span> <span class="pre">YOURPRIVATE.key</span> <span class="pre">-x509</span> <span class="pre">-days</span> <span class="pre">365</span> <span class="pre">-out</span> <span class="pre">YOURPUBLIC.pem</span> <span class="pre">-subj</span> <span class="pre">&quot;/C=US/ST=New</span> <span class="pre">York/L=Brooklyn/O=Example</span> <span class="pre">Brooklyn</span> <span class="pre">Company/CN=YOURDOMAIN.EXAMPLE&quot;</span></code></p>
10197
</div></blockquote>
10298
<p>[OR]</p>
10399
<p>Simply let <code class="docutils literal notranslate"><span class="pre">expose</span></code> create a self-signed SSL certificate and a private key.</p>
104100
<ul class="simple">
105-
<li><p><code class="docutils literal notranslate"><span class="pre">EMAIL_ADDRESS</span></code>: Email address to create the self-signed SSL and private key. Defaults to <code class="docutils literal notranslate"><span class="pre">USER&#64;expose-localhost.com</span></code></p></li>
106-
<li><p><code class="docutils literal notranslate"><span class="pre">ORGANIZATION</span></code>: Organization name for the certificate. Defaults to the AWS endpoint.</p></li>
101+
<li><p><strong>EMAIL_ADDRESS</strong>: Email address to create the self-signed SSL and private key. Defaults to <code class="docutils literal notranslate"><span class="pre">USER&#64;expose-localhost.com</span></code></p></li>
102+
<li><p><strong>ORGANIZATION</strong>: Organization name for the certificate. Defaults to the AWS endpoint.</p></li>
107103
</ul>
108104
</details></section>
109105
</section>
@@ -117,13 +113,24 @@ <h3>Installation<a class="headerlink" href="#installation" title="Permalink to t
117113
</section>
118114
<section id="tunneling">
119115
<h3>Tunneling:<a class="headerlink" href="#tunneling" title="Permalink to this heading"></a></h3>
120-
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">expose</span>
116+
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
121117

118+
<span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;env_file&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;custom&#39;</span> <span class="c1"># to load a custom .env file</span>
119+
120+
<span class="kn">import</span> <span class="nn">expose</span>
121+
122+
<span class="c1"># Instantiate object</span>
122123
<span class="n">tunnel</span> <span class="o">=</span> <span class="n">expose</span><span class="o">.</span><span class="n">Tunnel</span><span class="p">()</span>
123-
<span class="c1"># set &#39;purge&#39; flag to &#39;True&#39; to delete AWS resources if configuration fails</span>
124+
125+
<span class="c1"># Start tunneling</span>
124126
<span class="n">tunnel</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
127+
128+
<span class="c1"># set &#39;purge&#39; flag to &#39;True&#39; to delete AWS resources if configuration fails</span>
129+
<span class="c1"># tunnel.start(purge=True)</span>
130+
125131
<span class="c1"># sleep or do something else</span>
126-
<span class="c1"># set &#39;partial&#39; flag to &#39;True&#39; to skip SG deletion</span>
132+
133+
<span class="c1"># Stop tunneling - deletes all AWS resources acquired</span>
127134
<span class="n">tunnel</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
128135
</pre></div>
129136
</div>
@@ -144,19 +151,19 @@ <h2>Coding Standards<a class="headerlink" href="#coding-standards" title="Permal
144151
<section id="release-notes">
145152
<h2><a class="reference external" href="https://github.com/thevickypedia/expose/blob/main/release_notes.rst">Release Notes</a><a class="headerlink" href="#release-notes" title="Permalink to this heading"></a></h2>
146153
<p><strong>Requirement</strong></p>
147-
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>changelog-generator
154+
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>gitverse
148155
</pre></div>
149156
</div>
150157
<p><strong>Usage</strong></p>
151-
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>changelog<span class="w"> </span>reverse<span class="w"> </span>-f<span class="w"> </span>release_notes.rst<span class="w"> </span>-t<span class="w"> </span><span class="s1">&#39;Release Notes&#39;</span>
158+
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>gitverse-release<span class="w"> </span>reverse<span class="w"> </span>-f<span class="w"> </span>release_notes.rst<span class="w"> </span>-t<span class="w"> </span><span class="s1">&#39;Release Notes&#39;</span>
152159
</pre></div>
153160
</div>
154161
</section>
155162
<section id="linting">
156163
<h2>Linting<a class="headerlink" href="#linting" title="Permalink to this heading"></a></h2>
157164
<p><code class="docutils literal notranslate"><span class="pre">PreCommit</span></code> will ensure linting, and the doc creation are run on every commit.</p>
158165
<p><strong>Requirement</strong></p>
159-
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>pip<span class="w"> </span>install<span class="w"> </span><span class="nv">sphinx</span><span class="o">==</span><span class="m">5</span>.1.1<span class="w"> </span>pre-commit<span class="w"> </span>recommonmark<span class="w"> </span>gitverse
166+
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>pip<span class="w"> </span>install<span class="w"> </span><span class="nv">sphinx</span><span class="o">==</span><span class="m">5</span>.1.1<span class="w"> </span>pre-commit<span class="w"> </span>recommonmark
160167
</pre></div>
161168
</div>
162169
<p><strong>Usage</strong></p>

0 commit comments

Comments
 (0)