Skip to content

Commit 88423bf

Browse files
authored
Ensure bucket names are correctly prefixed (#718)
1 parent ee6159c commit 88423bf

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

controlpanel/api/models/s3bucket.py

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ class Meta:
5252
db_table = "control_panel_api_s3bucket"
5353
ordering = ('name',)
5454

55+
def __repr__(self):
56+
warehouse = ""
57+
if self.is_data_warehouse:
58+
warehouse = " (warehouse)"
59+
return f"<{self.__class__.__name__}: {self.name}{warehouse}>"
60+
5561
@property
5662
def arn(self):
5763
return s3_arn(self.name)

controlpanel/frontend/forms.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,20 @@ def clean_repo_url(self):
8181
return value
8282

8383

84+
def has_env_prefix(value):
85+
if not value.startswith(f'{settings.ENV}-'):
86+
raise ValidationError(
87+
f"Bucket name must be prefixed with {settings.ENV}-"
88+
)
89+
90+
8491
class CreateDatasourceForm(forms.Form):
8592
name = forms.CharField(
8693
max_length=60,
87-
validators=[RegexValidator(r'[a-z0-9.-]{1,60}')],
94+
validators=[
95+
has_env_prefix,
96+
RegexValidator(r'[a-z0-9.-]{1,60}'),
97+
],
8898
)
8999

90100

controlpanel/frontend/jinja2/datasource-create.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ <h1 class="govuk-heading-xl">{{ page_title }}</h1>
3030
},
3131
"classes": "govuk-!-width-two-thirds",
3232
"hint": {
33-
"text": '60 chars max, only lowercase letters, numbers, periods and hyphens, auto-prefixed with "' + env + '"'
33+
"text": '60 chars max, only lowercase letters, numbers, periods and hyphens, auto-prefixed with "' + env + '-"'
3434
},
3535
"name": "name",
3636
"attributes": {
37-
"data-bucket-prefix": env,
37+
"data-bucket-prefix": env + "-",
3838
"pattern": "[a-z0-9.-]{1,60}",
3939
"maxlength": "60",
4040
},
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
moj.Modules.bucketName = {
2-
inputName: 'new-datasource-name',
2+
selector: '[data-bucket-prefix]',
33

44
init() {
5-
this.$input = $(`#${this.inputName}`);
6-
7-
if (this.$input.length) {
8-
this.prefix = this.$input.data('bucket-prefix');
9-
this.bindEvents();
5+
const input = document.querySelector(this.selector);
6+
if (input) {
7+
this.bindEvents(input);
108
}
119
},
1210

13-
bindEvents() {
14-
this.$input.on('keypress blur', () => {
15-
this.formatBucketName();
16-
});
11+
bindEvents(input) {
12+
input.addEventListener('keypress', this.ensurePrefix.bind(input));
13+
input.addEventListener('blur', this.ensurePrefix.bind(input));
1714
},
1815

19-
formatBucketName() {
20-
let val = this.$input.val();
21-
22-
if (val.length < this.prefix.length) {
23-
val = this.prefix;
16+
ensurePrefix(e) {
17+
let val = this.value;
18+
if (val.length < this.dataset.bucketPrefix.length) {
19+
val = this.dataset.bucketPrefix;
2420
}
25-
26-
val = val.toLowerCase().replace(/ /gi, '-');
27-
this.$input.val(val);
21+
this.value = val.toLowerCase().replace(/ /gi, '-');
2822
},
2923
};

tests/frontend/views/test_datasource.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def detail(client, buckets, *args):
9999

100100
def create(client, *args):
101101
data = {
102-
'name': 'new_bucket',
102+
'name': 'test-new-bucket',
103103
}
104104
return client.post(reverse('create-datasource') + '?type=warehouse', data)
105105

0 commit comments

Comments
 (0)