Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send plugin configs to deck via settings.js #26

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public class Manifest {
public String name;
public String manifestVersion;
public List<String> jars;
public List<String> deck;
public Map<String, Object> options;

static final String regex = "^[a-zA-Z0-9]+\\/[\\w-]+$";
static final Pattern pattern = Pattern.compile(regex);

public void validate() throws HalException {

if (Stream.of(name, manifestVersion, jars).anyMatch(Objects::isNull)) {
if (Stream.of(name, manifestVersion, jars, deck).anyMatch(Objects::isNull)) {
throw new HalException(
new ConfigProblemBuilder(
Problem.Severity.FATAL, "Invalid plugin manifest, contains null values")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/

package com.netflix.spinnaker.halyard.core.resource.v1;

import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class ObjectReplaceTemplatedResource extends TemplatedResource {
public static Logger log = LoggerFactory.getLogger(ObjectReplaceTemplatedResource.class);

private String formatKey(String key) {
return "'{{%" + key + "%}}'";
}

private String getRegexSafeFormatKey() {
return "(?s).*\\'\\{\\{%.*%\\}\\}\\'.*";
}

@Override
public String toString() {
String contents = getContents();
for (Map.Entry<String, Object> binding : bindings.entrySet()) {
Object value = binding.getValue();
contents =
contents.replace(formatKey(binding.getKey()), value != null ? value.toString() : "");
}
if (contents.matches(getRegexSafeFormatKey())) {
log.warn(
"Found part of template that still contains a format key, likely a missing template value for a key, template: "
+ contents);
}
return contents;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.netflix.spinnaker.halyard.core.resource.v1;

import lombok.AccessLevel;
import lombok.Getter;

public class ObjectResource extends ObjectReplaceTemplatedResource {
public ObjectResource(String contents) {
this.contents = contents;
}

@Getter(AccessLevel.PROTECTED)
private final String contents;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.halyard.core.resource.v1

import spock.lang.Specification

class ObjectReplaceTemplatedResourceSpec extends Specification {
void "Attempt template with unfilled key"() {
setup:
String template = """'{{%some-unfilled-key%}}'"""
def logMock = Mock(org.slf4j.Logger)
ObjectReplaceTemplatedResource resource = (ObjectReplaceTemplatedResource)(new ObjectResource(template))
resource.log = logMock

when:
1 * logMock.warn(_) >> _
resource.toString()

then:
true
}

void "Attempt template with unfilled key multiline"() {
setup:
String template = """foo
'{{%some-unfilled-key%}}'
bar
"""
def logMock = Mock(org.slf4j.Logger)
ObjectReplaceTemplatedResource resource = (ObjectReplaceTemplatedResource)(new ObjectResource(template))
resource.log = logMock

when:
1 * logMock.warn(_) >> _
resource.toString()

then:
true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

package com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.deck;

import com.google.gson.Gson;
import com.netflix.spinnaker.halyard.config.model.v1.canary.Canary;
import com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration;
import com.netflix.spinnaker.halyard.config.model.v1.node.Features;
import com.netflix.spinnaker.halyard.config.model.v1.node.Notifications;
import com.netflix.spinnaker.halyard.config.model.v1.notifications.GithubStatusNotification;
import com.netflix.spinnaker.halyard.config.model.v1.notifications.SlackNotification;
import com.netflix.spinnaker.halyard.config.model.v1.notifications.TwilioNotification;
import com.netflix.spinnaker.halyard.config.model.v1.plugins.Manifest;
import com.netflix.spinnaker.halyard.config.model.v1.plugins.Plugin;
import com.netflix.spinnaker.halyard.config.model.v1.providers.appengine.AppengineProvider;
import com.netflix.spinnaker.halyard.config.model.v1.providers.aws.AwsAccount;
import com.netflix.spinnaker.halyard.config.model.v1.providers.aws.AwsProvider;
Expand All @@ -37,16 +40,15 @@
import com.netflix.spinnaker.halyard.config.services.v1.AccountService;
import com.netflix.spinnaker.halyard.config.services.v1.VersionsService;
import com.netflix.spinnaker.halyard.core.registry.v1.Versions;
import com.netflix.spinnaker.halyard.core.resource.v1.ObjectResource;
import com.netflix.spinnaker.halyard.core.resource.v1.StringResource;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerArtifact;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.RegistryBackedProfileFactory;
import com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -72,7 +74,11 @@ protected void setProfile(
Profile profile,
DeploymentConfiguration deploymentConfiguration,
SpinnakerRuntimeSettings endpoints) {
StringResource configTemplate = new StringResource(profile.getBaseContents());
ObjectResource configTemplate =
new ObjectResource(
profile
.getBaseContents()
.replace("version: version,", "version: version,\n plugins: '{{%plugins%}}',"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is hacky, but since we don't control the settings.js format, this works for now

UiSecurity uiSecurity = deploymentConfiguration.getSecurity().getUiSecurity();
profile.setUser(ApacheSettings.APACHE_USER);

Expand Down Expand Up @@ -232,8 +238,35 @@ protected void setProfile(
bindings.put("canary.showAllCanaryConfigs", canary.isShowAllConfigsEnabled());
}

// Configure Plugins
List<String> pluginBinding = new ArrayList<>();
List<Plugin> plugins = deploymentConfiguration.getPlugins().getPlugins();
Map<String, List<String>> pluginMetadata =
plugins.stream()
.filter(Plugin::getEnabled)
.filter(p -> !p.getManifestLocation().isEmpty())
.map(Plugin::generateManifest)
.collect(Collectors.toMap(Manifest::getName, Manifest::getDeck));
for (Map.Entry<String, List<String>> entry : pluginMetadata.entrySet()) {
for (String location : entry.getValue()) {
Map<String, String> resource = new HashMap<>();
resource.put("name", entry.getKey());
resource.put("location", location);
String extension = location.substring(location.lastIndexOf("."));
if (extension == "css") {
resource.put("type", "css");
} else {
resource.put("type", "js");
}
pluginBinding.add(new Gson().toJson(resource));
}
}
bindings.put("plugins", pluginBinding);

StringResource objectConfigTemplate =
new StringResource(configTemplate.setBindings(bindings).toString());
profile
.appendContents(configTemplate.setBindings(bindings).toString())
.appendContents(objectConfigTemplate.setBindings(bindings).toString())
.setRequiredFiles(backupRequiredFiles(uiSecurity, deploymentConfiguration.getName()));
}
}