Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ typings/

# next.js build output
.next
id_rsa
id_rsa.pub
package-lock.json
60 changes: 55 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# git-http-mock-server
# git-http-mock-server / git-ssh-mock-server
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.amrom.workers.dev%2Fisomorphic-git%2Fgit-http-mock-server.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.amrom.workers.dev%2Fisomorphic-git%2Fgit-http-mock-server?ref=badge_shield)


Clone and push to git repository test fixtures over HTTP.
Clone and push to git repository test fixtures over HTTP or SSH.

## What it does

Expand All @@ -22,6 +21,12 @@ It also supports HTTP Basic Auth password protection of repos so you can test ho

Using `isomorphic-git` and testing things from browsers? Fear not, `git-http-mock-server` includes appropriate CORS headers.

`git-ssh-mock-server` is similar, but because authentication happens before the client can say which repo
they are interested in, the authentication can't be customized per repository.
By default it allows anonymous SSH access. You can disable anonymous access and activate password authentication by setting the `GIT_SSH_MOCK_SERVER_PASSWORD` evironment variable.
(When password auth is activated, any username will work as long as the password matches the environment variable.)
Alternatively, you can set the `GIT_SSH_MOCK_SERVER_PUBKEY` environment variable to true to disable anonymous access and activate Public Key authentication. What key to use is explained in detail later in this document.

## How to use

```sh
Expand All @@ -44,6 +49,20 @@ Now in another shell, clone and push away...
> git clone http://localhost:8174/imaginatively-named-repo.git
```

To do the same thing but with SSH

```sh
> cd __fixtures__
> ls
test-repo1.git test-repo2.git imaginatively-named-repo.git
> git-ssh-mock-server
```

Now in another shell,
```sh
> git clone ssh://localhost:2222/imaginatively-named-repo.git
```

## Run in the background

If you want to reuse the same shell (as part of a shell script, for example)
Expand All @@ -58,14 +77,27 @@ you can run the server as a daemon in the background:
Just be sure to run `start` and `stop` from the same working directory.
(The `start` command writes the PID of the server to `./git-http-mock-server.pid` so that the `stop` command knows what process to kill.)

Same thing for SSH:

```sh
> git-ssh-mock-server start
> # do stuff
> git-ssh-mock-server stop
```

### Environment Variables

- `GIT_HTTP_MOCK_SERVER_PORT` default is 8174 (to be compatible with [git-http-server](https://github.com/bahamas10/node-git-http-server))
- `GIT_HTTP_MOCK_SERVER_ROUTE` default is `/`
- `GIT_HTTP_MOCK_SERVER_ROOT` default is `process.cwd()`
- `GIT_HTTP_MOCK_SERVER_ALLOW_ORIGIN` default is `*` (used for CORS)
- `GIT_SSH_MOCK_SERVER_PORT` default is 2222
- `GIT_SSH_MOCK_SERVER_ROUTE` default is `/`
- `GIT_SSH_MOCK_SERVER_ROOT` default is `process.cwd()`
- `GIT_SSH_MOCK_SERVER_PASSWORD` activate Password Authentication and use this password (leave blank to allow anonymous SSH access.)
- `GIT_SSH_MOCK_SERVER_PUBKEY` activate PubKey Authentication using the self-generated keypair (leave blank to allow anonymous SSH access.)

### .htpasswd support
### .htpasswd support (http-only)

You can place an Apache-style `.htpasswd` file in a bare repo to protect it with Basic Authentication.

Expand All @@ -80,23 +112,41 @@ testuser:$apr1$BRdvH4Mu$3HrpeyBrWiS88GcSPidgq/
If you don't have `htpasswd` on your machine, you can use [htpasswd](https://npm.im/htpasswd) which is
a cross-platform Node implementation of `htpasswd`.

### Public Key Auth support (ssh-only)

`git-ssh-mock-server` generates its own keypair using the system's native `ssh-keygen` the first time it's run,
in order to create encrypted SSH connections.
This key can be used to authenticate with the server as well!

1. Run `GIT_SSH_MOCK_SERVER_PUBKEY=true git-ssh-mock-server`
2. Try cloning (e.g. `git clone ssh://localhost:2222/imaginatively-named-repo.git`). It shouldn't work.
2. Run `git-ssh-mock-server exportKeys` which will copy the key files to `./id_rsa` and `./id_rsa.pub` in the working directory with the correct file permissions (`600`).
3. Run `ssh-add ./id_rsa`
4. Now try cloning. It works!
5. To clear the key from the ssh-agent, use `ssh-add -d ./id_rsa`

You can use `GIT_SSH_MOCK_SERVER_PUBKEY` and `GIT_SSH_MOCK_SERVER_PASSWORD` together, but using either one disables anonymous SSH access.

## Dependencies

- [basic-auth](https://ghub.io/basic-auth): node.js basic auth parser
- [buffer-equal-constant-time](https://ghub.io/buffer-equal-constant-time): Constant-time comparison of Buffers
- [chalk](https://ghub.io/chalk): Terminal string styling done right
- [fixturez](https://ghub.io/fixturez): Easily create and maintain test fixtures in the file system
- [git-http-backend](https://ghub.io/git-http-backend): serve a git repository over http
- [htpasswd-js](https://ghub.io/htpasswd-js): Pure JS htpasswd authentication
- [ssh2](https://ghub.io/ssh2): SSH2 client and server modules written in pure JavaScript for node.js

originally inspired by '[git-http-server](https://github.com/bahamas10/node-git-http-server)'

## License

MIT


[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.amrom.workers.dev%2Fisomorphic-git%2Fgit-http-mock-server.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.amrom.workers.dev%2Fisomorphic-git%2Fgit-http-mock-server?ref=badge_large)

## Changelog

1.2.0 - add SSH server
1.1.0 - support running in background and CORS headers
1.0.0 - Initial release
92 changes: 46 additions & 46 deletions daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,57 @@ const {spawn} = require('child_process')
const kill = require('tree-kill')
const minimisted = require('minimisted')

const cmdName = 'git-http-mock-server'
const target = require.resolve('./bin.js')
const args = [
target
]
module.exports = function (cmdName, target) {
const args = [
target
]

async function main({_: [cmd]}) {
switch (cmd) {
case 'start': {
require('daemonize-process')()
let server = spawn(
'node', args,
{
stdio: 'inherit',
windowsHide: true,
}
)
fs.writeFileSync(
path.join(process.cwd(), `${cmdName}.pid`),
String(process.pid),
'utf8'
)
process.on('exit', server.kill)
return
}
case 'stop': {
let pid
try {
pid = fs.readFileSync(
async function main({_: [cmd]}) {
switch (cmd) {
case 'start': {
require('daemonize-process')()
let server = spawn(
'node', args,
{
stdio: 'inherit',
windowsHide: true,
}
)
fs.writeFileSync(
path.join(process.cwd(), `${cmdName}.pid`),
String(process.pid),
'utf8'
);
} catch (err) {
console.log(`No ${cmdName}.pid file`)
)
process.on('exit', server.kill)
return
}
pid = parseInt(pid)
console.log('killing', pid)
kill(pid, (err) => {
if (err) {
console.log(err)
} else {
fs.unlinkSync(path.join(process.cwd(), `${cmdName}.pid`))
case 'stop': {
let pid
try {
pid = fs.readFileSync(
path.join(process.cwd(), `${cmdName}.pid`),
'utf8'
);
} catch (err) {
console.log(`No ${cmdName}.pid file`)
return
}
})
return
}
default: {
require(target)
pid = parseInt(pid)
console.log('killing', pid)
kill(pid, (err) => {
if (err) {
console.log(err)
} else {
fs.unlinkSync(path.join(process.cwd(), `${cmdName}.pid`))
}
})
return
}
default: {
require(target)
}
}
}
}

minimisted(main)
minimisted(main)
}
4 changes: 4 additions & 0 deletions http-daemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node
const cmdName = 'git-http-mock-server'
const target = require.resolve('./http-server.js')
require('./daemon.js')(cmdName, target)
0 bin.js → http-server.js
100644 → 100755
File renamed without changes.
57 changes: 55 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Clone and push to git repository test fixtures over HTTP.",
"main": "index.js",
"bin": {
"git-http-mock-server": "daemon.js"
"git-http-mock-server": "http-daemon.js",
"git-ssh-mock-server": "ssh-daemon.js"
},
"scripts": {
"test": "echo \"No tests\"",
Expand All @@ -29,14 +30,17 @@
"homepage": "https://github.com/isomorphic-git/git-http-mock-server#readme",
"dependencies": {
"basic-auth": "^2.0.0",
"buffer-equal-constant-time": "^1.0.1",
"chalk": "^2.4.1",
"daemonize-process": "^1.0.9",
"fixturez": "^1.1.0",
"git-http-backend": "^1.0.2",
"htpasswd-js": "^1.0.2",
"micro-cors": "^0.1.1",
"minimisted": "^2.0.0",
"tree-kill": "^1.2.0"
"tree-kill": "^1.2.0",
"ssh-keygen": "^0.4.2",
"ssh2": "^0.6.1"
},
"devDependencies": {
"semantic-release": "15.1.7",
Expand Down
4 changes: 4 additions & 0 deletions ssh-daemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node
const cmdName = 'git-ssh-mock-server'
const target = require.resolve('./ssh-server.js')
require('./daemon.js')(cmdName, target)
Loading