Skip to content

Commit 5a9d092

Browse files
committed
Merge pull request #160 from opdemand/fix-duplicate-sshkey
Looks good to me. I'll just have to gen a key when running the acceptance tests.
2 parents f9ef21f + b466011 commit 5a9d092

3 files changed

Lines changed: 193 additions & 1 deletion

File tree

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# -*- coding: utf-8 -*-
2+
import datetime
3+
from south.db import db
4+
from south.v2 import SchemaMigration
5+
from django.db import models
6+
7+
8+
class Migration(SchemaMigration):
9+
10+
def forwards(self, orm):
11+
# Adding unique constraint on 'Key', fields ['public']
12+
db.create_unique(u'api_key', ['public'])
13+
14+
15+
def backwards(self, orm):
16+
# Removing unique constraint on 'Key', fields ['public']
17+
db.delete_unique(u'api_key', ['public'])
18+
19+
20+
models = {
21+
u'api.build': {
22+
'Meta': {'ordering': "[u'-created']", 'unique_together': "((u'formation', u'uuid'),)", 'object_name': 'Build'},
23+
'checksum': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
24+
'config': ('api.fields.EnvVarsField', [], {'default': "u'null'", 'blank': 'True'}),
25+
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
26+
'dockerfile': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
27+
'formation': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Formation']"}),
28+
'output': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
29+
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
30+
'procfile': ('api.fields.ProcfileField', [], {'default': "u'null'", 'blank': 'True'}),
31+
'sha': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
32+
'size': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
33+
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
34+
'url': ('django.db.models.fields.URLField', [], {'max_length': '200'}),
35+
'uuid': ('api.fields.UuidField', [], {'unique': 'True', 'max_length': '32', 'primary_key': 'True'})
36+
},
37+
u'api.config': {
38+
'Meta': {'ordering': "[u'-created']", 'unique_together': "((u'formation', u'version'),)", 'object_name': 'Config'},
39+
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
40+
'formation': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Formation']"}),
41+
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
42+
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
43+
'uuid': ('api.fields.UuidField', [], {'unique': 'True', 'max_length': '32', 'primary_key': 'True'}),
44+
'values': ('api.fields.EnvVarsField', [], {'default': "u'{}'", 'blank': 'True'}),
45+
'version': ('django.db.models.fields.PositiveIntegerField', [], {})
46+
},
47+
u'api.container': {
48+
'Meta': {'ordering': "[u'created']", 'unique_together': "((u'formation', u'type', u'num'),)", 'object_name': 'Container'},
49+
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
50+
'formation': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Formation']"}),
51+
'node': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Node']"}),
52+
'num': ('django.db.models.fields.PositiveIntegerField', [], {}),
53+
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
54+
'status': ('django.db.models.fields.CharField', [], {'default': "u'up'", 'max_length': '64'}),
55+
'type': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
56+
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
57+
'uuid': ('api.fields.UuidField', [], {'unique': 'True', 'max_length': '32', 'primary_key': 'True'})
58+
},
59+
u'api.flavor': {
60+
'Meta': {'unique_together': "((u'owner', u'id'),)", 'object_name': 'Flavor'},
61+
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
62+
'id': ('django.db.models.fields.SlugField', [], {'max_length': '64'}),
63+
'init': ('api.fields.CloudInitField', [], {}),
64+
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
65+
'params': ('api.fields.ParamsField', [], {'default': "u'null'"}),
66+
'provider': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Provider']"}),
67+
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
68+
'uuid': ('api.fields.UuidField', [], {'unique': 'True', 'max_length': '32', 'primary_key': 'True'})
69+
},
70+
u'api.formation': {
71+
'Meta': {'unique_together': "((u'owner', u'id'),)", 'object_name': 'Formation'},
72+
'containers': ('json_field.fields.JSONField', [], {'default': "u'{}'", 'blank': 'True'}),
73+
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
74+
'id': ('django.db.models.fields.SlugField', [], {'max_length': '64'}),
75+
'layers': ('json_field.fields.JSONField', [], {'default': "u'{}'", 'blank': 'True'}),
76+
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
77+
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
78+
'uuid': ('api.fields.UuidField', [], {'unique': 'True', 'max_length': '32', 'primary_key': 'True'})
79+
},
80+
u'api.key': {
81+
'Meta': {'unique_together': "((u'owner', u'id'),)", 'object_name': 'Key'},
82+
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
83+
'id': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
84+
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
85+
'public': ('django.db.models.fields.TextField', [], {'unique': 'True'}),
86+
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
87+
'uuid': ('api.fields.UuidField', [], {'unique': 'True', 'max_length': '32', 'primary_key': 'True'})
88+
},
89+
u'api.layer': {
90+
'Meta': {'unique_together': "((u'formation', u'id'),)", 'object_name': 'Layer'},
91+
'chef_version': ('django.db.models.fields.CharField', [], {'default': "u'11.4.4'", 'max_length': '32'}),
92+
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
93+
'environment': ('django.db.models.fields.CharField', [], {'default': "u'_default'", 'max_length': '64'}),
94+
'flavor': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Flavor']"}),
95+
'formation': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Formation']"}),
96+
'id': ('django.db.models.fields.SlugField', [], {'max_length': '64'}),
97+
'initial_attributes': ('json_field.fields.JSONField', [], {'default': "u'{}'", 'blank': 'True'}),
98+
'level': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
99+
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
100+
'run_list': ('django.db.models.fields.CharField', [], {'max_length': '512'}),
101+
'ssh_private_key': ('django.db.models.fields.TextField', [], {}),
102+
'ssh_public_key': ('django.db.models.fields.TextField', [], {}),
103+
'ssh_username': ('django.db.models.fields.CharField', [], {'default': "u'ubuntu'", 'max_length': '64'}),
104+
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
105+
'uuid': ('api.fields.UuidField', [], {'unique': 'True', 'max_length': '32', 'primary_key': 'True'})
106+
},
107+
u'api.node': {
108+
'Meta': {'unique_together': "((u'formation', u'id'),)", 'object_name': 'Node'},
109+
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
110+
'formation': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Formation']"}),
111+
'fqdn': ('django.db.models.fields.CharField', [], {'max_length': '256', 'null': 'True', 'blank': 'True'}),
112+
'id': ('django.db.models.fields.CharField', [], {'max_length': '64'}),
113+
'layer': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Layer']"}),
114+
'num': ('django.db.models.fields.PositiveIntegerField', [], {}),
115+
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
116+
'provider_id': ('django.db.models.fields.SlugField', [], {'max_length': '64', 'null': 'True', 'blank': 'True'}),
117+
'status': ('api.fields.NodeStatusField', [], {'default': "u'null'", 'null': 'True', 'blank': 'True'}),
118+
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
119+
'uuid': ('api.fields.UuidField', [], {'unique': 'True', 'max_length': '32', 'primary_key': 'True'})
120+
},
121+
u'api.provider': {
122+
'Meta': {'unique_together': "((u'owner', u'id'),)", 'object_name': 'Provider'},
123+
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
124+
'creds': ('api.fields.CredentialsField', [], {'default': "u'null'", 'blank': 'True'}),
125+
'id': ('django.db.models.fields.SlugField', [], {'max_length': '64'}),
126+
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
127+
'type': ('django.db.models.fields.SlugField', [], {'max_length': '16'}),
128+
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
129+
'uuid': ('api.fields.UuidField', [], {'unique': 'True', 'max_length': '32', 'primary_key': 'True'})
130+
},
131+
u'api.release': {
132+
'Meta': {'ordering': "[u'-created']", 'unique_together': "((u'formation', u'version'),)", 'object_name': 'Release'},
133+
'build': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Build']", 'null': 'True', 'blank': 'True'}),
134+
'config': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Config']"}),
135+
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
136+
'formation': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['api.Formation']"}),
137+
'image': ('django.db.models.fields.CharField', [], {'default': "u'deis/buildstep'", 'max_length': '256'}),
138+
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
139+
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
140+
'uuid': ('api.fields.UuidField', [], {'unique': 'True', 'max_length': '32', 'primary_key': 'True'}),
141+
'version': ('django.db.models.fields.PositiveIntegerField', [], {})
142+
},
143+
u'auth.group': {
144+
'Meta': {'object_name': 'Group'},
145+
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
146+
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
147+
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
148+
},
149+
u'auth.permission': {
150+
'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'},
151+
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
152+
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
153+
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
154+
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
155+
},
156+
u'auth.user': {
157+
'Meta': {'object_name': 'User'},
158+
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
159+
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
160+
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
161+
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
162+
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
163+
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
164+
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
165+
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
166+
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
167+
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
168+
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
169+
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
170+
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
171+
},
172+
u'contenttypes.contenttype': {
173+
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
174+
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
175+
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
176+
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
177+
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
178+
}
179+
}
180+
181+
complete_apps = ['api']

api/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Key(UuidAuditedModel):
7171

7272
owner = models.ForeignKey(settings.AUTH_USER_MODEL)
7373
id = models.CharField(max_length=128)
74-
public = models.TextField()
74+
public = models.TextField(unique=True)
7575

7676
class Meta:
7777
verbose_name = 'SSH Key'

api/tests/key.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,14 @@ def test_key(self):
4040
self.assertEqual(body['public'], response.data['public'])
4141
response = self.client.delete(url)
4242
self.assertEqual(response.status_code, 204)
43+
44+
def test_key_duplicate(self):
45+
"""
46+
Test that a user cannot add a duplicate key
47+
"""
48+
url = '/api/keys'
49+
body = {'id': 'mykey@box.local', 'public': 'ssh-rsa XXX'}
50+
response = self.client.post(url, json.dumps(body), content_type='application/json')
51+
self.assertEqual(response.status_code, 201)
52+
response = self.client.post(url, json.dumps(body), content_type='application/json')
53+
self.assertEqual(response.status_code, 400)

0 commit comments

Comments
 (0)