Skip to content

Commit

Permalink
misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi1693 committed May 7, 2023
1 parent e8c7aed commit fd2a22a
Show file tree
Hide file tree
Showing 26 changed files with 255 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length = 160
extend-ignore = E203
extend-ignore = E203, E266, F403, F405, W503
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,3 @@ repos:
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.32.2
hooks:
- id: markdownlint
2 changes: 2 additions & 0 deletions configuration/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# Based on https://github.com/netbox-community/netbox/blob/master/netbox/netbox/configuration.example.py

# Read secret from file


def _read_secret(secret_name, default=None):
try:
f = open('/run/secrets/' + secret_name, encoding='utf-8')
Expand Down
4 changes: 2 additions & 2 deletions docs/rest-api/working-with-secrets.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ steps are needed to encrypt or decrypt secret data.
## Generating a Session Key

In order to encrypt or decrypt secret data, a session key must be attached to the API request. To generate a session key,
send an authenticated request to the `/api/plugins/secrets/get-session-key/` endpoint with the private RSA key which
send an authenticated request to the `/api/plugins/secrets/session-keys/` endpoint with the private RSA key which
matches your [UserKey](../models/userkey.md). The private key must be POSTed with the name `private_key`.

```no-highlight
$ curl -X POST http://netbox/api/plugins/secrets/get-session-key/ \
$ curl -X POST http://netbox/api/plugins/secrets/session-keys/ \
-H "Authorization: Token $TOKEN" \
-H "Accept: application/json; indent=4" \
--data-urlencode "private_key@<filename>"
Expand Down
20 changes: 19 additions & 1 deletion netbox_secrets/api/nested_serializers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from netbox.api.serializers import WritableNestedSerializer
from rest_framework import serializers

from netbox_secrets.models import Secret, SecretRole
from ..models import *

__all__ = [
'NestedSecretRoleSerializer',
'NestedSecretSerializer',
'NestedSessionKeySerializer',
'NestedUserKeySerializer',
]


Expand All @@ -24,3 +26,19 @@ class NestedSecretRoleSerializer(WritableNestedSerializer):
class Meta:
model = SecretRole
fields = ['id', 'url', 'display', 'name', 'slug', 'secret_count']


class NestedSessionKeySerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_secrets-api:sessionkey-detail')

class Meta:
model = SessionKey
fields = ['id', 'url', 'display']


class NestedUserKeySerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_secrets-api:userkey-detail')

class Meta:
model = UserKey
fields = ['id', 'url', 'display']
78 changes: 74 additions & 4 deletions netbox_secrets/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from django.contrib.contenttypes.models import ContentType
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers

from netbox.api.fields import ContentTypeField
from netbox.api.serializers import NetBoxModelSerializer
from netbox.constants import NESTED_SERIALIZER_PREFIX
from rest_framework import serializers
from utilities.api import get_serializer_for_model
from .nested_serializers import *

from ..constants import SECRET_ASSIGNABLE_MODELS
from ..models import Secret, SecretRole, UserKey
from ..models import *
from .nested_serializers import *

__all__ = [
'SecretRoleSerializer',
'SecretSerializer',
'SessionKeySerializer',
'SessionKeyCreateSerializer',
'UserKeySerializer',
'RSAKeyPairSerializer',
]


#
Expand All @@ -17,18 +26,24 @@


class UserKeySerializer(serializers.ModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_secrets-api:userkey-detail')
public_key = serializers.CharField()
private_key = serializers.CharField(
write_only=True,
)

display = serializers.SerializerMethodField(read_only=True)

is_active = serializers.BooleanField(read_only=True)

is_filled = serializers.BooleanField(read_only=True)

class Meta:
model = UserKey
fields = [
'pk',
'id',
'url',
'display',
'public_key',
'private_key',
Expand All @@ -38,10 +53,57 @@ class Meta:
'is_filled',
]

@extend_schema_field(serializers.CharField())
def get_display(self, obj):
return str(obj)


#
# Session Keys
#


class SessionKeySerializer(serializers.ModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_secrets-api:sessionkey-detail')

display = serializers.SerializerMethodField(read_only=True)

userkey = NestedUserKeySerializer()

class Meta:
model = SessionKey
fields = [
'pk',
'id',
'url',
'display',
'userkey',
'created',
]

@extend_schema_field(serializers.CharField())
def get_display(self, obj):
return str(obj)


class SessionKeyCreateSerializer(serializers.ModelSerializer):
private_key = serializers.CharField(
write_only=True,
)

preserve_key = serializers.BooleanField(
default=False,
write_only=True,
)

class Meta:
model = SessionKey
fields = [
'private_key',
'preserve_key',
]


#
# Secret Roles
#
Expand All @@ -60,6 +122,7 @@ class Meta:
'name',
'slug',
'description',
'comments',
'custom_fields',
'created',
'last_updated',
Expand Down Expand Up @@ -92,6 +155,8 @@ class Meta:
'name',
'plaintext',
'hash',
'description',
'comments',
'tags',
'custom_fields',
'created',
Expand All @@ -116,3 +181,8 @@ def validate(self, data):
super().validate(data)

return data


class RSAKeyPairSerializer(serializers.Serializer):
public_key = serializers.CharField()
private_key = serializers.CharField()
5 changes: 1 addition & 4 deletions netbox_secrets/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
router = NetBoxRouter()
router.APIRootView = views.SecretsRootView

# User Key
router.register('user-keys', views.UserKeyViewSet)

# Secrets
router.register('session-keys', views.SessionKeyViewSet)
router.register('secret-roles', views.SecretRoleViewSet)
router.register('secrets', views.SecretViewSet)

# Miscellaneous
router.register('get-session-key', views.GetSessionKeyViewSet, basename='get-session-key')
router.register('generate-rsa-key-pair', views.GenerateRSAKeyPairViewSet, basename='generate-rsa-key-pair')

urlpatterns = router.urls
Loading

0 comments on commit fd2a22a

Please sign in to comment.