Skip to content

Commit f99e9ab

Browse files
Added max aliases field to set a valid value
1 parent 56431d2 commit f99e9ab

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleDescribable.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.util.Arrays;
1010
import java.util.LinkedList;
11+
import java.util.Map;
1112

1213
public interface AnsibleDescribable extends Describable {
1314

@@ -90,6 +91,9 @@ public static String[] getValues() {
9091
}
9192
}
9293

94+
// General variables
95+
String SECONDARY = "SECONDARY";
96+
9397
public static final String SERVICE_PROVIDER_TYPE = "ansible-service";
9498
public static final String ANSIBLE_PLAYBOOK_PATH = "ansible-playbook";
9599
public static final String ANSIBLE_PLAYBOOK_INLINE = "ansible-playbook-inline";
@@ -158,7 +162,14 @@ public static String[] getValues() {
158162

159163
public static final String ANSIBLE_ENCRYPT_EXTRA_VARS = "ansible-encrypt-extra-vars";
160164

161-
String ANSIBLE_YAML_DATA_SIZE = "ansible-yaml-data-size";
165+
// Inventory Yaml
166+
String ANSIBLE_YAML_DATA_SIZE = "ansible-yaml-data-size";
167+
String ANSIBLE_YAML_MAX_ALIASES = "ansible-yaml-max-aliases";
168+
String INVENTORY_YAML = "Inventory Yaml";
169+
Map<String, Object> inventoryYamlOpt = Map.of(
170+
StringRenderingConstants.GROUPING, SECONDARY,
171+
StringRenderingConstants.GROUP_NAME, INVENTORY_YAML
172+
);
162173

163174
public static Property PLAYBOOK_PATH_PROP = PropertyUtil.string(
164175
ANSIBLE_PLAYBOOK_PATH,
@@ -533,9 +544,20 @@ public static String[] getValues() {
533544
Property YAML_DATA_SIZE_PROP = PropertyBuilder.builder()
534545
.integer(ANSIBLE_YAML_DATA_SIZE)
535546
.required(false)
536-
.title("Inventory Yaml Data Size")
537-
.description("Set the MB size (Default value is 10)"+
538-
" therefore, the plugin can process the yaml data response coming from Ansible."+
547+
.title("Data Size")
548+
.description("Set the MB size (Default value is 10)."+
549+
" Allows the plugin to process the yaml data response coming from Ansible."+
550+
" (This only applies when Gather Facts = No)")
551+
.renderingOptions(inventoryYamlOpt)
552+
.build();
553+
554+
Property YAML_MAX_ALIASES_PROP = PropertyBuilder.builder()
555+
.integer(ANSIBLE_YAML_MAX_ALIASES)
556+
.required(false)
557+
.title("Max Aliases")
558+
.description("Set max size (Default value is 1000)."+
559+
" Allows to set the maximum number of aliases that the inventory can have."+
539560
" (This only applies when Gather Facts = No)")
561+
.renderingOptions(inventoryYamlOpt)
540562
.build();
541563
}

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleResourceModelSource.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.rundeck.plugins.ansible.plugin;
22

33
import com.dtolabs.rundeck.core.common.Framework;
4-
import com.dtolabs.rundeck.core.common.INodeEntry;
54
import com.dtolabs.rundeck.core.common.INodeSet;
65
import com.dtolabs.rundeck.core.common.NodeEntryImpl;
76
import com.dtolabs.rundeck.core.common.NodeSetImpl;
@@ -28,6 +27,7 @@
2827
import com.rundeck.plugins.ansible.util.VaultPrompt;
2928
import lombok.Setter;
3029
import lombok.extern.slf4j.Slf4j;
30+
import org.apache.commons.lang.StringUtils;
3131
import org.rundeck.app.spi.Services;
3232
import org.rundeck.storage.api.PathUtil;
3333
import org.rundeck.storage.api.StorageException;
@@ -49,16 +49,16 @@
4949
import java.nio.file.SimpleFileVisitor;
5050
import java.nio.file.attribute.BasicFileAttributes;
5151
import java.util.ArrayList;
52-
import java.util.Arrays;
5352
import java.util.HashMap;
5453
import java.util.HashSet;
5554
import java.util.List;
5655
import java.util.Map;
5756
import java.util.Map.Entry;
5857
import java.util.Properties;
5958
import java.util.Set;
60-
import java.util.stream.Collectors;
6159

60+
import static com.rundeck.plugins.ansible.ansible.AnsibleDescribable.ANSIBLE_YAML_DATA_SIZE;
61+
import static com.rundeck.plugins.ansible.ansible.AnsibleDescribable.ANSIBLE_YAML_MAX_ALIASES;
6262
import static com.rundeck.plugins.ansible.ansible.InventoryList.ALL;
6363
import static com.rundeck.plugins.ansible.ansible.InventoryList.CHILDREN;
6464
import static com.rundeck.plugins.ansible.ansible.InventoryList.HOSTS;
@@ -84,8 +84,6 @@ public class AnsibleResourceModelSource implements ResourceModelSource, ProxyRun
8484

8585
private String inventory;
8686
private boolean gatherFacts;
87-
@Setter
88-
private Integer yamlDataSize;
8987
private boolean ignoreErrors = false;
9088
private String limit;
9189
private String ignoreTagPrefix;
@@ -132,6 +130,11 @@ public class AnsibleResourceModelSource implements ResourceModelSource, ProxyRun
132130

133131
protected boolean encryptExtraVars = false;
134132

133+
@Setter
134+
private Integer yamlDataSize;
135+
@Setter
136+
private Integer yamlMaxAliases;
137+
135138
@Setter
136139
private AnsibleInventoryList.AnsibleInventoryListBuilder ansibleInventoryListBuilder = null;
137140

@@ -141,7 +144,7 @@ public AnsibleResourceModelSource(final Framework framework) {
141144
this.framework = framework;
142145
}
143146

144-
private static String resolveProperty(
147+
private static String resolveProperty(
145148
final String attribute,
146149
final String defaultValue,
147150
final Properties configuration,
@@ -194,8 +197,6 @@ public void configure(Properties configuration) throws ConfigurationException {
194197
gatherFacts = "true".equals(resolveProperty(AnsibleDescribable.ANSIBLE_GATHER_FACTS,null,configuration,executionDataContext));
195198
ignoreErrors = "true".equals(resolveProperty(AnsibleDescribable.ANSIBLE_IGNORE_ERRORS,null,configuration,executionDataContext));
196199

197-
yamlDataSize = resolveIntProperty(AnsibleDescribable.ANSIBLE_YAML_DATA_SIZE,10, configuration, executionDataContext);
198-
199200
limit = (String) resolveProperty(AnsibleDescribable.ANSIBLE_LIMIT,null,configuration,executionDataContext);
200201
ignoreTagPrefix = (String) resolveProperty(AnsibleDescribable.ANSIBLE_IGNORE_TAGS,null,configuration,executionDataContext);
201202

@@ -251,6 +252,10 @@ public void configure(Properties configuration) throws ConfigurationException {
251252

252253
encryptExtraVars = "true".equals(resolveProperty(AnsibleDescribable.ANSIBLE_ENCRYPT_EXTRA_VARS,"false",configuration,executionDataContext));
253254

255+
// Inventory Yaml
256+
yamlDataSize = resolveIntProperty(ANSIBLE_YAML_DATA_SIZE,10, configuration, executionDataContext);
257+
yamlMaxAliases = resolveIntProperty(ANSIBLE_YAML_MAX_ALIASES,1000, configuration, executionDataContext);
258+
254259
}
255260

256261
public AnsibleRunner.AnsibleRunnerBuilder buildAnsibleRunner() throws ResourceModelSourceException {
@@ -704,10 +709,14 @@ public void ansibleInventoryList(NodeSetImpl nodes, AnsibleRunner.AnsibleRunnerB
704709
LoaderOptions snakeOptions = new LoaderOptions();
705710
// max inventory file size allowed to 10mb
706711
snakeOptions.setCodePointLimit(codePointLimit);
712+
// max aliases. Default value is 1000
713+
snakeOptions.setMaxAliasesForCollections(yamlMaxAliases);
707714
Yaml yaml = new Yaml(new SafeConstructor(snakeOptions));
708715

709716
String listResp = getNodesFromInventory(runnerBuilder);
710717

718+
validateAliases(listResp);
719+
711720
Map<String, Object> allInventory;
712721
try {
713722
allInventory = yaml.load(listResp);
@@ -967,4 +976,15 @@ private boolean isTagMapValid(Map<String, Object> tagMap, String tagName) {
967976
return true;
968977
}
969978

979+
/**
980+
* Validates whether the YAML content contains aliases that exceed the maximum allowed.
981+
* @param content String yaml
982+
*/
983+
public void validateAliases(String content) {
984+
int totalAliases = StringUtils.countMatches(content, ": *");
985+
if (totalAliases > yamlMaxAliases) {
986+
log.warn("The yaml inventory received has {} aliases and the maximum allowed is {}.", totalAliases, yamlMaxAliases);
987+
}
988+
}
989+
970990
}

src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleResourceModelSourceFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public AnsibleResourceModelSourceFactory(final Framework framework) {
3535
builder.property(INVENTORY_PROP);
3636
builder.property(CONFIG_FILE_PATH);
3737
builder.property(GATHER_FACTS_PROP);
38-
builder.property(YAML_DATA_SIZE_PROP);
3938
builder.property(IGNORE_ERRORS_PROP);
4039
builder.property(LIMIT_PROP);
4140
builder.property(DISABLE_LIMIT_PROP);
@@ -64,6 +63,9 @@ public AnsibleResourceModelSourceFactory(final Framework framework) {
6463
builder.property(SSH_USE_AGENT);
6564
builder.property(BECOME_PASSWORD_STORAGE_PROP);
6665

66+
builder.property(YAML_DATA_SIZE_PROP);
67+
builder.property(YAML_MAX_ALIASES_PROP);
68+
6769
builder.mapping(ANSIBLE_INVENTORY,PROJ_PROP_PREFIX + ANSIBLE_INVENTORY);
6870
builder.frameworkMapping(ANSIBLE_INVENTORY,FWK_PROP_PREFIX + ANSIBLE_INVENTORY);
6971
builder.mapping(ANSIBLE_CONFIG_FILE_PATH,PROJ_PROP_PREFIX + ANSIBLE_CONFIG_FILE_PATH);

0 commit comments

Comments
 (0)