Skip to content

Commit

Permalink
Merge branch 'main' into local-time
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-elinardi authored Oct 25, 2023
2 parents afc7f0c + be9144e commit 0bab6ec
Show file tree
Hide file tree
Showing 18 changed files with 2,506 additions and 101 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pb.go linguist-generated
go.sum linguist-generated
66 changes: 36 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A non-interactive daemon for host management

```mermaid
flowchart LR;
subgraph sanssh ["sansshell client (sanssh)"]
cli;
client;
Expand Down Expand Up @@ -55,11 +55,12 @@ audited in advance or after the fact, and is ideally deterministic (for a given
state of the local machine).

sanssh is a simple CLI with a friendly API for dumping debugging state and
interacting with a remote machine. It also includes a set of convenient but
interacting with a remote machine. It also includes a set of convenient but
perhaps-less-friendly subcommands to address the raw SansShell API endpoints.

# Getting Started
How to set up, build and run locally for testing. All commands are relative to

How to set up, build and run locally for testing. All commands are relative to
the project root directory.

Building SansShell requires a recent version of Go (check the go.mod file for
Expand All @@ -74,6 +75,7 @@ $ cp -r auth/mtls/testdata ~/.sansshell
```

Then you can build and run the server, in separate terminal windows:

```
$ go run ./cmd/sansshell-server
$ go run ./cmd/sanssh --targets=localhost file read /etc/hosts
Expand All @@ -96,6 +98,7 @@ buffer compiler (`protoc`) (version 3 or above) as well as the protoc plugins
for Go and Go-GRPC

On MacOS, the protocol buffer can be installed via homebrew using

```
brew install protobuf
```
Expand All @@ -105,7 +108,7 @@ directly installing a release version from the [protocol buffers github][1]

## Environment setup : protoc plugins.

On any platform, Once protoc has been installed, you can install the required
On any platform, once protoc has been installed, you can install the required
code generation plugins using `go install`.

```
Expand All @@ -126,17 +129,11 @@ $ go generate tools.go

## Creating your own certificates

As an alternative to copying auth/mtls/testdata, you can create your onwn example mTLS certs:
```
$ go install github.com/meterup/generate-cert@latest
$ mkdir -m 0700 certs
$ cd certs
$ $(go env GOPATH)/bin/generate-cert --host=localhost,127.0.0.1,::1
$ cd ../
$ ln -s $(pwd)/certs ~/.sansshell
```
As an alternative to copying auth/mtls/testdata, you can create your own example mTLS certs. See the
[mtls testdata readme](/auth/mtls/testdata/README.md) for steps.

## Debugging

Reflection is included in the RPC servers (proxy and sansshell-server)
allowing for the use of [grpc_cli](https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md).

Expand All @@ -152,25 +149,29 @@ $ GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=$HOME/.sansshell/root.pem grpc_cli \
NOTE: This connects to the proxy. Change to 50042 if you want to connect to the sansshell-server.

# A tour of the codebase

SansShell is composed of 5 primary concepts:
1. A series of services, which live in the `services/` directory.
1. A server which wraps these services into a local host agent.
1. A proxy server which can be used as an entry point to processing sansshell
RPCs by validating policy and then doing fanout to 1..N actual
sansshell servers. This can be done as a one to many RPC where
a single incoming RPC is replicated to N backend hosts in one RPC call.
1. A reference server binary, which includes all of the services.
1. A CLI, which serves as the reference implementation of how to use the
services via the agent.

1. A series of services, which live in the `services/` directory.
1. A server which wraps these services into a local host agent.
1. A proxy server which can be used as an entry point to processing sansshell
RPCs by validating policy and then doing fanout to 1..N actual
sansshell servers. This can be done as a one to many RPC where
a single incoming RPC is replicated to N backend hosts in one RPC call.
1. A reference server binary, which includes all of the services.
1. A CLI, which serves as the reference implementation of how to use the
services via the agent.

## Services

Services implement at least one gRPC API endpoint, and expose it by calling
`RegisterSansShellService` from `init()`. The goal is to allow custom
`RegisterSansShellService` from `init()`. The goal is to allow custom
implementations of the SansShell Server to easily import services they wish to
use, and have zero overhead or risk from services they do not import at compile
time.

### List of available Services:

1. Ansible: Run a local ansible playbook and return output
1. Execute: Execute a command
1. HealthCheck
Expand All @@ -180,29 +181,32 @@ time.
1. Process operations: List, Get stacks (native or Java), Get dumps (core or Java heap)
1. Service operations: List, Status, Start/stop/restart


TODO: Document service/.../client expectations.

## The Server class

Most of the logic of instantiating a local SansShell server lives in the
`server` directory. This instantiates a gRPC server, registers the imported
`server` directory. This instantiates a gRPC server, registers the imported
services with that server, and constraints them with the supplied OPA policy.

## The reference Proxy Server binary

There is a reference implementation of a SansShell Proxy Server in
`cmd/proxy-server`, which should be suitable as-written for many use cases.
It's intentionally kept relatively short, so that it can be copied to another
repository and customized by adjusting only the imported services.

## The reference Server binary

There is a reference implementation of a SansShell Server in
`cmd/sansshell-server`, which should be suitable as-written for many use cases.
It's intentionally kept relatively short, so that it can be copied to another
repository and customized by adjusting only the imported services.

## The reference CLI client

There is a reference implementation of a SansShell CLI Client in
`cmd/sanssh`. It provides raw access to each gRPC endpoint, as well
`cmd/sanssh`. It provides raw access to each gRPC endpoint, as well
as a way to implement "convenience" commands which chain together a series of
actions.

Expand All @@ -219,11 +223,12 @@ complete -C /path/to/sanssh -o dirnames sanssh
```

# Extending SansShell
SansShell is built on a principle of "Don't pay for what you don't use". This

SansShell is built on a principle of "Don't pay for what you don't use". This
is advantageous in both minimizing the resources of SansShell server (binary
size, memory footprint, etc) as well as reducing the security risk of running
it. To accomplish that, all of the SansShell services are independent modules,
which can be optionally included at build time. The reference server and
it. To accomplish that, all of the SansShell services are independent modules,
which can be optionally included at build time. The reference server and
client provide access to the features of all of the built-in modules, and come
with exposure to all of their potential bugs and bloat.

Expand All @@ -236,6 +241,7 @@ That same extensibility makes it easy to add additional functionality by
implementing your own module.

To quickly rebuild all binaries you can run:

```
$ go generate build.go
```
Expand All @@ -244,7 +250,7 @@ and they will be placed in a bin directory (which is ignored by git).

TODO: Add example client and server, building in different SansShell modules.

If you need to edit a proto file (to augment an existing service or
If you need to edit a proto file (to augment an existing service or
create a new one) you'll need to generate proto outputs.

```
Expand Down
57 changes: 49 additions & 8 deletions auth/mtls/mtls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,52 @@ allow {
input.peer.net.network = "something else"
}
`
allowPeerSerialPolicy = `
allowPeerCommonName = `
package sansshell.authz
default allow = false
allow {
input.type = "google.protobuf.Empty"
input.method = "/HealthCheck.HealthCheck/Ok"
input.peer.cert.subject.SerialNumber = "18469474828185269013890292403630281187"
input.peer.cert.subject.CommonName = "sanssh"
}
`
denyPeerSerialPolicy = `
denyPeerCommonName = `
package sansshell.authz
default allow = false
allow {
input.type = "google.protobuf.Empty"
input.method = "/HealthCheck.HealthCheck/Ok"
input.peer.cert.subject.SerialNumber = "12345"
input.peer.cert.subject.CommonName = "not-sanssh"
}
`

allowPeerPrincipal = `
package sansshell.authz
default allow = false
allow {
input.type = "google.protobuf.Empty"
input.method = "/HealthCheck.HealthCheck/Ok"
input.peer.principal.id = "sanssh"
input.peer.principal.groups[_] = "group2"
}
`

denyPeerPrincipal = `
package sansshell.authz
default allow = false
allow {
input.type = "google.protobuf.Empty"
input.method = "/HealthCheck.HealthCheck/Ok"
input.peer.principal.id = "sanssh"
input.peer.principal.groups[_]= "group3"
}
`
)
Expand Down Expand Up @@ -144,6 +170,7 @@ func serverWithPolicy(t *testing.T, policy string) (*bufconn.Listener, *grpc.Ser
server.WithCredentials(creds),
server.WithPolicy(policy),
server.WithAuthzHook(rpcauth.HostNetHook(lis.Addr())),
server.WithAuthzHook(rpcauth.PeerPrincipalFromCertHook()),
)
testutil.FatalOnErr("Could not build server", err, t)
listening := make(chan struct{})
Expand Down Expand Up @@ -302,13 +329,23 @@ func TestHealthCheck(t *testing.T) {
err: "OPA policy does not permit this request",
},
{
name: "allowed peer by subject serial",
policy: allowPeerSerialPolicy,
name: "allowed peer by subject common name",
policy: allowPeerCommonName,
err: "",
},
{
name: "denied peer by subject common name",
policy: denyPeerCommonName,
err: "OPA policy does not permit this request",
},
{
name: "allowed peer by principal parsed from cert",
policy: allowPeerPrincipal,
err: "",
},
{
name: "denied peer by subject serial",
policy: denyPeerSerialPolicy,
name: "denied peer by principal parsed from cert",
policy: denyPeerPrincipal,
err: "OPA policy does not permit this request",
},
} {
Expand All @@ -331,6 +368,10 @@ func TestHealthCheck(t *testing.T) {
}
return
}
if err == nil && tc.err != "" {
t.Errorf("Passed, but expected error: %v", tc.err)

}
t.Logf("Response: %+v", resp)
s.GracefulStop()
})
Expand Down
63 changes: 63 additions & 0 deletions auth/mtls/testdata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# mtls testdata

This directory contains some test certificates.

If you need to regenerate them, here's some helpful commands. Openssl

## Generating private keys

These commands will generate new private keys.

```bash
openssl genrsa -out client.key
openssl genrsa -out leaf.key
openssl genrsa -out root.key
```

## Generating certificates

These commands will generate new certificates. We need to generate certificate signing requests as part of generating certificates, but we can delete them afterwards.

```bash
# CA cert
openssl req -new -key root.key -out root.csr -subj "/O=Acme Co"
openssl x509 -req -days 30000 -in root.csr -signkey root.key -out root.pem

# Cert for the client
openssl req -new -key client.key -out client.csr -subj "/O=Acme Co/OU=group1/OU=group2/CN=sanssh"
openssl x509 -req -days 3000 -in client.csr -CA root.pem -CAkey root.key -out client.pem -extensions req_ext -extfile /dev/stdin <<EOF
[req_ext]
keyUsage = critical, digitalSignature
extendedKeyUsage = clientAuth
basicConstraints = critical, CA:FALSE
EOF

# Cert for the server and/or proxy
openssl req -new -key leaf.key -out leaf.csr -subj "/O=Acme Co/OU=group2/OU=group3/CN=sansshell-server"
openssl x509 -req -days 3000 -in leaf.csr -CA root.pem -CAkey root.key -out leaf.pem -extensions req_ext -extfile /dev/stdin <<EOF
[req_ext]
keyUsage = critical, digitalSignature
extendedKeyUsage = serverAuth
basicConstraints = critical, CA:FALSE
subjectAltName = @alt_names
[alt_names]
DNS.0 = localhost
DNS.1 = bufnet
IP.0 = 127.0.0.1
IP.1 = ::1
EOF

# Cleanup
rm root.csr client.csr leaf.csr
```

## Viewing contents

These commands will print out the information embedded in the certificates.

```bash
openssl x509 -in client.pem -text -noout
openssl x509 -in leaf.pem -text -noout
openssl x509 -in root.pem -text -noout
```
22 changes: 11 additions & 11 deletions auth/mtls/testdata/client.pem
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-----BEGIN CERTIFICATE-----
MII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