Skip to content

Commit

Permalink
Move to internal (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
KCarretto authored and hulto committed Sep 30, 2023
1 parent ab8f848 commit 9e074d7
Show file tree
Hide file tree
Showing 169 changed files with 647 additions and 652 deletions.
12 changes: 6 additions & 6 deletions docs/_docs/dev-guide/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Introduction
tags:
tags:
- Dev Guide
description: Read this before contributing to Realm!
permalink: dev-guide/introduction
Expand All @@ -15,7 +15,7 @@ Realm is under heavy active development and documentation can go stale quickly i

We will do our best during code review to catch changes that require documentation updates, but sometimes things will slip by. If you notice a discrepancy between our codebase and the documentation, please kindly [file an issue](https://github.com/kcarretto/realm/issues/new?labels=documentation&title=Documentation%20Discrepancy:&body=Please%20include%20the%20location%20of%20the%20inaccurate%20documentation%20and%20a%20helpful%20description%20of%20what%20needs%20improvement.) to track it or submit a PR to correct it. You can use the ["Edit this page"](https://github.com/kcarretto/realm/edit/main/docs/_docs/dev-guide/introduction.md) feature in the right navbar of the documentation to quickly navigate to the appropriate section of documentation that requires an update.

### Testing
### Testing

Realm contains code across a variety of languages and frameworks. Testing helps ensure our codebase remains stable, enables us to refactor and develop new features with confidence, and can help new Realm developers understand the expected behavior of our code. Below is an outline of the testing requirements for each portion of the codebase, as well as guidance on testing best practices.

Expand Down Expand Up @@ -60,7 +60,7 @@ A Task represents a set of instructions to perform on a Target system. For examp
References malicious code or persistence mechanisms that are deployed to compromise target systems. Some configuration information for Implants (e.g. `ImplantConfigs`) can be managed through Tavern.

### Agent
An Agent is a type of implant which retrieves execution instructions by connecting to our backend infrastructure (calling back) and querying for new tasks.
An Agent is a type of implant which retrieves execution instructions by connecting to our backend infrastructure (calling back) and querying for new tasks.

# Project Structure
* **[.devcontainer](https://github.com/KCarretto/realm/tree/main/.devcontainer)** contains settings required for configuring a VSCode dev container that can be used for Realm development
Expand All @@ -75,9 +75,9 @@ An Agent is a type of implant which retrieves execution instructions by connecti
* **[implants/lib/tavern](https://github.com/KCarretto/realm/tree/main/implants/lib/tavern)** is the source of our agents graphql API to interface with Tavern (Rust)
* **[tavern](https://github.com/KCarretto/realm/tree/main/tavern)** is the parent folder of Tavern related code and packages, and stores the `main.go` executable for the service
* **[tavern/auth](https://github.com/KCarretto/realm/tree/main/tavern/auth)** is a package for managing authentication for Tavern, and is used by various packages that rely on obtaining viewer information
* **[tavern/ent](https://github.com/KCarretto/realm/tree/main/tavern/ent)** contains models and related code for interacting with the database (most of this is code generated by **[entgo](https://entgo.io/))**
* **[tavern/ent/schema](https://github.com/KCarretto/realm/tree/main/tavern/ent/schema)** contains the schema definitions for our DB models
* **[tavern/graphql](https://github.com/KCarretto/realm/tree/main/tavern/graphql)** contains our GraphQL definitions and resolvers (most of this code is generated by **[entgo](https://entgo.io/)** and **[gqlgen](https://github.com/99designs/gqlgen))**
* **[tavern/internal/ent](https://github.com/KCarretto/realm/tree/main/tavern/internal/ent)** contains models and related code for interacting with the database (most of this is code generated by **[entgo](https://entgo.io/))**
* **[tavern/internal/ent/schema](https://github.com/KCarretto/realm/tree/main/tavern/internal/ent/schema)** contains the schema definitions for our DB models
* **[tavern/internal/graphql](https://github.com/KCarretto/realm/tree/main/tavern/internal/graphql)** contains our GraphQL definitions and resolvers (most of this code is generated by **[entgo](https://entgo.io/)** and **[gqlgen](https://github.com/99designs/gqlgen))**
* **[tavern/internal](https://github.com/KCarretto/realm/tree/main/tavern/internal)** contains various internal packages that makeup Tavern
* **[tavern/internal/www](https://github.com/KCarretto/realm/tree/main/tavern/internal/www)** contains Tavern's UI code
* **[terraform](https://github.com/KCarretto/realm/tree/main/terraform)** contains the Terraform used to deploy a production ready Realm instance. See [Tavern User Guide](https://docs.realm.pub/user-guide/tavern) to learn how to use.
Expand Down
26 changes: 13 additions & 13 deletions docs/_docs/dev-guide/tavern.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,33 +214,33 @@ query get_task_res {
### Creating a New Model

1. Initialize the schema `cd tavern && go run entgo.io/ent/cmd/ent init <NAME>`
2. Update the generated file in `tavern/ent/schema/<NAME>.go`
2. Update the generated file in `tavern/internal/ent/schema/<NAME>.go`
3. Ensure you include a `func (<NAME>) Annotations() []schema.Annotation` method which returns a `entgql.QueryField()` annotation to tell entgo to generate a GraphQL root query for this model (if you'd like it to be queryable from the root query)
4. Update `tavern/graphql/gqlgen.yml` to include the ent types in the `autobind:` section (e.g.`- github.com/kcarretto/realm/tavern/ent/<NAME>`)
5. **Optionally** update the `models:` section of `tavern/graphql/gqlgen.yml` to bind any GraphQL enum types to their respective `entgo` generated types (e.g. `github.com/kcarretto/realm/tavern/ent/<NAME>.<ENUM_FIELD>`)
4. Update `tavern/internal/graphql/gqlgen.yml` to include the ent types in the `autobind:` section (e.g.`- github.com/kcarretto/realm/tavern/internal/ent/<NAME>`)
5. **Optionally** update the `models:` section of `tavern/internal/graphql/gqlgen.yml` to bind any GraphQL enum types to their respective `entgo` generated types (e.g. `github.com/kcarretto/realm/tavern/internal/ent/<NAME>.<ENUM_FIELD>`)
6. Run `go generate ./tavern/...` from the project root
7. If you added an annotation for a root query field (see above), you will notice auto-generated the `query.resolvers.go` file has been updated with new methods to query your model (e.g. `func (r *queryResolver) <NAME>s ...`)
* This must be implemented (e.g. `return r.client.<NAME>.Query().All(ctx)` where NAME is the name of your model)

### Adding Mutations

1. Update the `mutation.graphql` schema file to include your new mutation and please include it in the section for the model it's mutating if applicable (e.g. createUser should be defined near all the related User mutations)
* **Note:** Input types such as `Create<NAME>Input` or `Update<NAME>Input` will already be generated if you [added the appropriate annotations to your ent schema](https://entgo.io/docs/tutorial-todo-gql#install-and-configure-entgql). If you require custom input mutations (e.g. `ClaimTasksInput`) then add them to the `inputs.graphql` file (Golang code will be generated in tavern/graphql/models e.g. `models.ClaimTasksInput`).
* **Note:** Input types such as `Create<NAME>Input` or `Update<NAME>Input` will already be generated if you [added the appropriate annotations to your ent schema](https://entgo.io/docs/tutorial-todo-gql#install-and-configure-entgql). If you require custom input mutations (e.g. `ClaimTasksInput`) then add them to the `inputs.graphql` file (Golang code will be generated in tavern/internal/graphql/models e.g. `models.ClaimTasksInput`).
2. Run `go generate ./...`
3. Implement generated the generated mutation resolver method in `tavern/graphql/mutation.resolvers.go`
3. Implement generated the generated mutation resolver method in `tavern/internal/graphql/mutation.resolvers.go`
* Depending on the mutation you're trying to implement, a one liner such as `return r.client.<NAME>.Create().SetInput(input).Save(ctx)` might be sufficient
4. Please write a unit test for your new mutation by defining YAML test cases in a new `testdata/mutations` subdirectory with your mutations name (e.g. `tavern/graphql/testdata/mutations/mymutation/SomeTest.yml`)
4. Please write a unit test for your new mutation by defining YAML test cases in a new `testdata/mutations` subdirectory with your mutations name (e.g. `tavern/internal/graphql/testdata/mutations/mymutation/SomeTest.yml`)

### Code Generation Reference

* After making a change, remember to run `go generate ./...` from the project root.
* `tavern/ent/schema` is a directory which defines our graph using database models (ents) and the relations between them
* `tavern/internal/ent/schema` is a directory which defines our graph using database models (ents) and the relations between them
* `tavern/generate.go` is responsible for generating ents defined by the ent schema as well as updating the GraphQL schema and generating related code
* `tavern/ent/entc.go` is responsible for generating code for the entgo <-> 99designs/gqlgen GraphQL integration
* `tavern/graphql/schema/mutation.graphql` defines all mutations supported by our API
* `tavern/graphql/schema/query.graphql` is a GraphQL schema automatically generated by ent, providing useful types derived from our ent schemas as well as root-level queries defined by entgo annotations
* `tavern/graphql/schema/scalars.graphql` defines scalar GraphQL types that can be used to help with Go bindings (See [gqlgen docs](https://gqlgen.com/reference/scalars/) for more info)
* `tavern/graphql/schema/inputs.graphql` defines custom GraphQL inputs that can be used with your mutations (e.g. outside of the default auto-generated CRUD inputs)
* `tavern/internal/ent/entc.go` is responsible for generating code for the entgo <-> 99designs/gqlgen GraphQL integration
* `tavern/internal/graphql/schema/mutation.graphql` defines all mutations supported by our API
* `tavern/internal/graphql/schema/query.graphql` is a GraphQL schema automatically generated by ent, providing useful types derived from our ent schemas as well as root-level queries defined by entgo annotations
* `tavern/internal/graphql/schema/scalars.graphql` defines scalar GraphQL types that can be used to help with Go bindings (See [gqlgen docs](https://gqlgen.com/reference/scalars/) for more info)
* `tavern/internal/graphql/schema/inputs.graphql` defines custom GraphQL inputs that can be used with your mutations (e.g. outside of the default auto-generated CRUD inputs)

### YAML Test Reference

Expand Down Expand Up @@ -274,7 +274,7 @@ This however restricts the available transport methods the agent may use to comm

### GraphQL Example

GraphQL mutations enable clients to _mutate_ or modify backend data. Tavern supports a variety of different mutations for interacting with the graph ([see schema](https://github.com/KCarretto/realm/blob/main/tavern/graphql/schema/mutation.graphql)). The two mutations agents rely on are `claimTasks` and `submitTaskResult` (covered in more detail below). GraphQL requests are submitted as HTTP POST requests to Tavern, with a JSON body including the GraphQL mutation. Below is an example JSON body that may be sent to the Tavern GraphQL API:
GraphQL mutations enable clients to _mutate_ or modify backend data. Tavern supports a variety of different mutations for interacting with the graph ([see schema](https://github.com/KCarretto/realm/blob/main/tavern/internal/graphql/schema/mutation.graphql)). The two mutations agents rely on are `claimTasks` and `submitTaskResult` (covered in more detail below). GraphQL requests are submitted as HTTP POST requests to Tavern, with a JSON body including the GraphQL mutation. Below is an example JSON body that may be sent to the Tavern GraphQL API:

```json
{
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,12 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.24.4 h1:0gyJJEBYtCV87zI/x2nZCPyDxD51K6xM8SkwjHFCNEU=
github.com/vektah/gqlparser/v2 v2.5.1 h1:ZGu+bquAY23jsxDRcYpWjttRZrUz07LbiY77gUOHcr4=
github.com/vektah/gqlparser/v2 v2.5.1/go.mod h1:mPgqFBu/woKTVYWyNk8cO3kh4S/f4aRFZrvOnp3hmCs=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/zclconf/go-cty v1.13.0 h1:It5dfKTTZHe9aeppbNOda3mN7Ag7sg6QkBNm6TkyFa0=
github.com/zclconf/go-cty v1.13.0/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
86 changes: 43 additions & 43 deletions implants/lib/tavern/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Beacon implements Node {
tasks: [Task!]
}
"""BeaconHostPlatform is enum for the field host_platform"""
enum BeaconHostPlatform @goModel(model: "github.com/kcarretto/realm/tavern/ent/beacon.HostPlatform") {
enum BeaconHostPlatform @goModel(model: "github.com/kcarretto/realm/tavern/internal/ent/beacon.HostPlatform") {
Windows
Linux
MacOS
Expand Down Expand Up @@ -337,6 +337,47 @@ input FileWhereInput {
hashEqualFold: String
hashContainsFold: String
}
"""
An object with an ID.
Follows the [Relay Global Object Identification Specification](https://relay.dev/graphql/objectidentification.htm)
"""
interface Node @goModel(model: "github.com/kcarretto/realm/tavern/internal/ent.Noder") {
"""The id of the object."""
id: ID!
}
"""Possible directions in which to order a list of items when provided an `orderBy` argument."""
enum OrderDirection {
"""Specifies an ascending order for a given `orderBy` argument."""
ASC
"""Specifies a descending order for a given `orderBy` argument."""
DESC
}
"""
Information about pagination in a connection.
https://relay.dev/graphql/connections.htm#sec-undefined.PageInfo
"""
type PageInfo {
"""When paginating forwards, are there more items?"""
hasNextPage: Boolean!
"""When paginating backwards, are there more items?"""
hasPreviousPage: Boolean!
"""When paginating backwards, the cursor to continue."""
startCursor: Cursor
"""When paginating forwards, the cursor to continue."""
endCursor: Cursor
}
type Query {
"""Fetches an object given its ID."""
node(
"""ID of the object."""
id: ID!
): Node
"""Lookup nodes by a list of IDs."""
nodes(
"""The list of node IDs."""
ids: [ID!]!
): [Node]!
}
type Quest implements Node {
id: ID!
"""Timestamp of when this ent was created"""
Expand Down Expand Up @@ -443,47 +484,6 @@ input QuestWhereInput {
hasCreator: Boolean
hasCreatorWith: [UserWhereInput!]
}
"""
An object with an ID.
Follows the [Relay Global Object Identification Specification](https://relay.dev/graphql/objectidentification.htm)
"""
interface Node @goModel(model: "github.com/kcarretto/realm/tavern/ent.Noder") {
"""The id of the object."""
id: ID!
}
"""Possible directions in which to order a list of items when provided an `orderBy` argument."""
enum OrderDirection {
"""Specifies an ascending order for a given `orderBy` argument."""
ASC
"""Specifies a descending order for a given `orderBy` argument."""
DESC
}
"""
Information about pagination in a connection.
https://relay.dev/graphql/connections.htm#sec-undefined.PageInfo
"""
type PageInfo {
"""When paginating forwards, are there more items?"""
hasNextPage: Boolean!
"""When paginating backwards, are there more items?"""
hasPreviousPage: Boolean!
"""When paginating backwards, the cursor to continue."""
startCursor: Cursor
"""When paginating forwards, the cursor to continue."""
endCursor: Cursor
}
type Query {
"""Fetches an object given its ID."""
node(
"""ID of the object."""
id: ID!
): Node
"""Lookup nodes by a list of IDs."""
nodes(
"""The list of node IDs."""
ids: [ID!]!
): [Node]!
}
type Tag implements Node {
id: ID!
"""Name of the tag"""
Expand All @@ -493,7 +493,7 @@ type Tag implements Node {
beacons: [Beacon!]
}
"""TagKind is enum for the field kind"""
enum TagKind @goModel(model: "github.com/kcarretto/realm/tavern/ent/tag.Kind") {
enum TagKind @goModel(model: "github.com/kcarretto/realm/tavern/internal/ent/tag.Kind") {
group
service
}
Expand Down
8 changes: 4 additions & 4 deletions tavern/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (
"os"

"entgo.io/contrib/entgql"
"github.com/kcarretto/realm/tavern/graphql"
"github.com/kcarretto/realm/tavern/internal/graphql"
"github.com/kcarretto/realm/tavern/tomes"

gqlgraphql "github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/kcarretto/realm/tavern/auth"
"github.com/kcarretto/realm/tavern/ent"
"github.com/kcarretto/realm/tavern/ent/migrate"
"github.com/kcarretto/realm/tavern/internal/auth"
"github.com/kcarretto/realm/tavern/internal/cdn"
"github.com/kcarretto/realm/tavern/internal/ent"
"github.com/kcarretto/realm/tavern/internal/ent/migrate"
"github.com/kcarretto/realm/tavern/internal/www"
"github.com/urfave/cli"
)
Expand Down
2 changes: 1 addition & 1 deletion tavern/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"time"

"github.com/kcarretto/realm/tavern/ent"
"github.com/kcarretto/realm/tavern/internal/ent"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"

Expand Down
4 changes: 2 additions & 2 deletions tavern/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"testing"

"github.com/kcarretto/realm/tavern/ent/migrate"
"github.com/kcarretto/realm/tavern/ent/tag"
"github.com/kcarretto/realm/tavern/internal/ent/migrate"
"github.com/kcarretto/realm/tavern/internal/ent/tag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
Expand Down
8 changes: 4 additions & 4 deletions tavern/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"testing"

tavern "github.com/kcarretto/realm/tavern"
"github.com/kcarretto/realm/tavern/ent"
"github.com/kcarretto/realm/tavern/ent/beacon"
"github.com/kcarretto/realm/tavern/ent/enttest"
"github.com/kcarretto/realm/tavern/ent/quest"
"github.com/kcarretto/realm/tavern/internal/ent"
"github.com/kcarretto/realm/tavern/internal/ent/beacon"
"github.com/kcarretto/realm/tavern/internal/ent/enttest"
"github.com/kcarretto/realm/tavern/internal/ent/quest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
35 changes: 0 additions & 35 deletions tavern/ent/entc.go

This file was deleted.

8 changes: 0 additions & 8 deletions tavern/generate.go

This file was deleted.

4 changes: 2 additions & 2 deletions tavern/auth/context.go → tavern/internal/auth/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"

"github.com/kcarretto/realm/tavern/ent"
"github.com/kcarretto/realm/tavern/ent/user"
"github.com/kcarretto/realm/tavern/internal/ent"
"github.com/kcarretto/realm/tavern/internal/ent/user"
)

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
"time"

"github.com/kcarretto/realm/tavern/ent"
"github.com/kcarretto/realm/tavern/internal/ent"
)

// AuthDisabledMiddleware should only be used when authentication has been disabled.
Expand Down
Loading

0 comments on commit 9e074d7

Please sign in to comment.