use of com.google.api.services.compute.model.Firewall in project cloudbreak by hortonworks.
the class GcpFirewallInResourceBuilder method createNewFirewallRule.
private ComputeRequest<Operation> createNewFirewallRule(GcpContext context, AuthenticatedContext auth, Group group, Security security, CloudResource buildableResource, String projectId) throws IOException {
ComputeRequest<Operation> firewallRequest;
List<String> sourceRanges = getSourceRanges(security);
Firewall firewall = new Firewall();
firewall.setSourceRanges(sourceRanges);
List<Allowed> allowedRules = new ArrayList<>();
allowedRules.add(new Allowed().setIPProtocol("icmp"));
allowedRules.addAll(createRule(security));
firewall.setTargetTags(Collections.singletonList(GcpStackUtil.getGroupClusterTag(auth.getCloudContext(), group)));
firewall.setAllowed(allowedRules);
firewall.setName(buildableResource.getName());
firewall.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", projectId, context.getParameter(GcpNetworkResourceBuilder.NETWORK_NAME, String.class)));
return context.getCompute().firewalls().insert(projectId, firewall);
}
use of com.google.api.services.compute.model.Firewall in project cloudbreak by hortonworks.
the class GcpFirewallInResourceBuilder method updateExistingFirewallForNewTargets.
private Update updateExistingFirewallForNewTargets(GcpContext context, AuthenticatedContext auth, Group group, Security security) throws java.io.IOException {
Firewall firewall = context.getCompute().firewalls().get(context.getProjectId(), security.getCloudSecurityId()).execute();
if (firewall.getTargetTags() == null) {
firewall.setTargetTags(Lists.newArrayListWithCapacity(1));
}
firewall.getTargetTags().add(GcpStackUtil.getGroupClusterTag(auth.getCloudContext(), group));
return context.getCompute().firewalls().update(context.getProjectId(), firewall.getName(), firewall);
}
use of com.google.api.services.compute.model.Firewall in project platformlayer by platformlayer.
the class EnsureFirewallIngress method handler.
@Handler
public void handler(GoogleCloud cloud, GoogleComputeMachine machine) throws OpsException {
GoogleComputeClient client = googleComputeClientFactory.getComputeClient(cloud);
// Find the public address, although the Google Cloud firewall may be blocking it
publicAddress = machine.getNetworkPoint().getBestAddress(NetworkPoint.forPublicInternet());
String serverLink = machine.getServerSelfLink();
List<Firewall> rules = client.getInstanceFirewallRules(serverLink);
Firewall matchingRule = findMatchingRule(rules);
if (OpsContext.isConfigure()) {
if (matchingRule == null) {
Firewall rule = new Firewall();
rule.setSourceRanges(Arrays.asList("0.0.0.0/0"));
rule.setName("pl-" + UUID.randomUUID().toString());
Allowed allowed = new Allowed();
allowed.setIPProtocol("tcp");
allowed.setPorts(Arrays.asList("" + model.publicPort));
rule.setAllowed(Arrays.asList(allowed));
rule.setNetwork(client.buildNetworkUrl("default"));
client.createFirewallRule(rule);
}
}
if (OpsContext.isDelete()) {
if (matchingRule != null) {
client.deleteFirewallRule(matchingRule);
}
}
}
use of com.google.api.services.compute.model.Firewall in project platformlayer by platformlayer.
the class EnsureFirewallIngress method findMatchingRule.
private Firewall findMatchingRule(List<Firewall> rules) {
for (Firewall rule : rules) {
List<Allowed> allowedList = rule.getAllowed();
boolean matchesPortAndProtocol = false;
if (allowedList != null) {
for (Allowed allowed : allowedList) {
if (!Objects.equal("tcp", allowed.getIPProtocol())) {
continue;
}
List<String> ports = allowed.getPorts();
if (ports != null) {
for (String port : ports) {
if (port.contains("-")) {
if (port.equals(model.publicPort + "-" + model.publicPort)) {
matchesPortAndProtocol = true;
}
} else {
if (port.equals(model.publicPort + "")) {
matchesPortAndProtocol = true;
}
}
}
}
}
}
if (!matchesPortAndProtocol) {
continue;
}
boolean matchedSourceRange = false;
List<String> sourceRanges = rule.getSourceRanges();
if (sourceRanges == null) {
if (rule.getSourceTags() == null) {
matchedSourceRange = true;
}
} else {
for (String sourceRange : sourceRanges) {
if (Objects.equal(sourceRange, "0.0.0.0/0")) {
matchedSourceRange = true;
}
}
}
if (matchedSourceRange) {
return rule;
}
}
return null;
}
use of com.google.api.services.compute.model.Firewall in project cloudbreak by hortonworks.
the class GcpPlatformResources method securityGroups.
@Override
public CloudSecurityGroups securityGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws IOException {
Compute compute = GcpStackUtil.buildCompute(cloudCredential);
String projectId = GcpStackUtil.getProjectId(cloudCredential);
Map<String, Set<CloudSecurityGroup>> result = new HashMap<>();
if (compute != null) {
FirewallList firewallList = compute.firewalls().list(projectId).execute();
for (Firewall firewall : firewallList.getItems()) {
Map<String, Object> properties = new HashMap<>();
properties.put("network", getNetworkName(firewall));
CloudSecurityGroup cloudSecurityGroup = new CloudSecurityGroup(firewall.getName(), firewall.getName(), properties);
result.computeIfAbsent(region.value(), k -> new HashSet<>()).add(cloudSecurityGroup);
}
}
return new CloudSecurityGroups(result);
}
Aggregations