Skip to content

Commit

Permalink
SSH Connection OK
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolman-Freecss committed Oct 15, 2024
1 parent a49e8c7 commit de975d2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
27 changes: 27 additions & 0 deletions .run/Execute_main.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Execute_main" type="PythonConfigurationType" factoryName="Python">
<module name="DevOps-Jenkins-Shellscripting" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
<env name="ENV" value="local" />
</envs>
<option name="SDK_HOME" value="" />
<option name="SDK_NAME" value="Python 3.10 (KF_EnvOrchestatorTool)" />
<option name="WORKING_DIRECTORY" value="D:\Data\ProjectsData\Programming\Projects\Web\_Devops\KF_EnvOrchestatorTool\src\local\main" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/local/main/main.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
</component>
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ This tool is used to serve an automated environment in local or cloud platform t
- [Local installation](#local-installation)
- [Configure Jenkins](#configure-jenkins)
- [Configure Environment Variables to execute main.py](#configure-environment-variables-to-execute-mainpy)
- [Configure SSH](#configure-ssh)
- [AWS Configuration](#aws-configuration)
- [Trigger Terraform pipeline](#trigger-terraform-pipeline)
- [Connect to EC2 instance](#connect-to-ec2-instance)
- [Configuration](#configuration)
- [Dependencies](#dependencies)
- [Tech stacks CI/CD](#tech-stacks-ci/cd)
- [Tips](#tips)
- [Troubleshoting](#troubleshoting)
- [Jenkins](#jenkins)
- [Scripts](#scripts)
- [AWS](#aws)
- [Settings](#settings)

# Systems

Expand Down Expand Up @@ -56,6 +59,23 @@ JENKINS_PASS=<YOUR_JENKINS_PASSWORD>
ACCESS_TOKEN=<YOUR_GITHUB_ACCESS_TOKEN>
```

### Configure SSH

- Install OpenSSH Server on your local machine.
- Start the service.

```bash
# Windows
Start-Service sshd
```

- (Optional) Try to connect from container to your local machine with SSH.

```bash
docker exec -it jenkins-git bash
ssh -i /var/jenkins_home/.ssh/id_rsa [email protected] -vvv
```

## AWS Configuration

Implantation of Jenkins automated with Terraform on AWS.
Expand Down Expand Up @@ -175,6 +195,11 @@ docker push kolmanfreecss/jenkins-git
- Shellscript
- Terraform

# Tips

- Check Event Viewer on Windows to see if SSH Server is running properly.
- `Applications and Services Logs > OpenSSH > Operational`

# Troubleshoting

## Jenkins
Expand Down Expand Up @@ -223,6 +248,29 @@ docker push kolmanfreecss/jenkins-git
- Remove permissions to other group users or another users because AWS won't let you connect to the EC2 instance if
the permissions are too permissive.
## Settings
- Install SSH Server on local machine.
- If you have a Windows Server you can follow Microsoft official documentation to install OpenSSH Server. Or check https://github.com/PowerShell/Win32-OpenSSH/releases
- Also you could use WSL to install OpenSSH Server.
- ```bash
sudo apt-get install openssh-server
```
- Configure permissions to the id_rsa file to not be too permissive.
- ```bash
chmod 600 /var/jenkins_home/.ssh/id_rsa
```
- Create an authorized_keys file in the .ssh folder with the public key of the local machine.
- ```bash
cat /var/jenkins_home/.ssh/id_rsa.pub >> /var/jenkins_home/.ssh/authorized_keys
```
- Create an sshd_config file in the .ssh folder with the following IMPORTANT configurations UNCCOMMENTED.:
- ```bash
Port 22
AuthorizedKeysFile /var/jenkins_home/.ssh/authorized_keys
PubkeyAuthentication yes
PasswordAuthentication no
```
---
Shield: [![CC-BY-NC-ND 4.0][CC-BY-NC-ND-shield]][CC-BY-NC-ND]
Expand Down
3 changes: 1 addition & 2 deletions src/local/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ services:
build:
context: . # Path where the Dockerfile is located
dockerfile: Dockerfile # Name of the Dockerfile
image: kolmanfreecss/jenkins-git:latest
container_name: kf-jenkins
user: root
privileged: true
ports:
- "8080:8080" # UI Jenkins
- "50000:50000" # Jenkins agents
- "50001:50000" # Jenkins agents
environment:
- TINI_SUBREAPER=1 # Register Tini as a subreaper (to not need to use Tini as PID 1)
volumes:
Expand Down
12 changes: 7 additions & 5 deletions src/local/main/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ def build_user_credentials() -> any:
"""

id_value = f"{config_module.config.get(config_module.ConfigKeys.JENKINS_CREDENTIALS_ID)}-user"
description_value = f"Credentials to access GitHub with PAT with {id_value}"
credentials = f'''<?xml version='1.1' encoding='UTF-8'?>
<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
<scope>GLOBAL</scope>
<id>{id_value}</id>
<username>{config_module.config.get(config_module.ConfigKeys.JENKINS_USER)}</username>
<password>{config_module.config.get(config_module.ConfigKeys.PAT_JENKINS)}</password>
<description>Credentials to access GitHub with PAT</description>
<description>{description_value}</description>
</com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
'''
return credentials
Expand All @@ -70,6 +71,7 @@ def build_ssh_credentials(force: bool = False) -> any:
private_key = get_ssh(force)

id_value = f"{config_module.config.get(config_module.ConfigKeys.AGENT_CREDENTIALS_SSH)}"
description_value = f"SSH Credentials to access GitHub with {id_value}"
credentials = f'''<?xml version='1.1' encoding='UTF-8'?>
<com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey>
<scope>GLOBAL</scope>
Expand All @@ -78,10 +80,10 @@ def build_ssh_credentials(force: bool = False) -> any:
<privateKeySource class="com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource">
<privateKey>{private_key}</privateKey>
</privateKeySource>
<description>SSH Credentials to access GitHub</description>
<description>{description_value}</description>
</com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey>
'''
print(f'BUILD SSH CREDENTIALS:: XML Document: {credentials}')
print(f'BUILD SSH CREDENTIALS:: XML Document: ***** ') # {credentials}')
return credentials


Expand Down Expand Up @@ -120,10 +122,10 @@ def build_credentials(credential_type: CredentialsType, force: bool = False) ->
if response.status_code == 200:
print('Credentials created successfully')
elif not force:
print(f'Error creating credentials status code: {response.status_code}, message: {response.text}, \n retrying with force flag')
print(f'ERROR creating credentials status code: {response.status_code}, message: {response.text}, \n retrying with force flag')
build_credentials(credential_type, force=True)
else:
print(f'Error creating credentials status code: {response.status_code}, message: {response.text}')
print(f'ERROR creating credentials status code: {response.status_code}, message: {response.text}')

def get_ssh(force: bool = False) -> str:
"""
Expand Down

0 comments on commit de975d2

Please sign in to comment.