Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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 org.apache.whirr.service;
 
 
 import java.util.List;
 import java.util.Set;
 
 
 public class FirewallManager {
 
   public static class Rule {
     
     public static Rule create() {
       return new Rule();
     }
     
     private String source;
     private Set<Instancedestinations;
     private int[] ports;
     
     private Rule() {
     }
    
    

Parameters:
source The allowed source IP for traffic. If not set, this will default to org.apache.whirr.ClusterSpec.getClientCidrs(), or, if that is not set, to the client's originating IP.
 
     public Rule source(String source) {
       this. = source;
       return this;
     }
    
    

Parameters:
destination The allowed destination instance.
 
     public Rule destination(Instance destination) {
       this. = Collections.singleton(destination);
       return this;
     }
    
    

Parameters:
destinations The allowed destination instances.
 
     public Rule destination(Set<Instancedestinations) {
       this. = destinations;
       return this;
     }
    
    

Parameters:
destinationPredicate A predicate which is used to evaluate the allowed destination instances.
 
     public Rule destination(Predicate<InstancedestinationPredicate) {
       this. = destinationPredicate;
       return this;
     }
    
    

Parameters:
port The port on the destination which is to be opened. Overrides any previous calls to port(int) or ports(int[]).
 
     public Rule port(int port) {
       this. = new int[] { port };
       return this;
     }
    
    

Parameters:
ports The ports on the destination which are to be opened. Overrides any previous calls to port(int) or ports(int[]).
    public Rule ports(int... ports) {
      this. = ports;
      return this;
    }
  }
  private static final Logger LOG = LoggerFactory
      .getLogger(FirewallManager.class);
  private Cluster cluster;
  public FirewallManager(ComputeServiceContext computeServiceContext,
      ClusterSpec clusterSpecCluster cluster) {
    this. = computeServiceContext;
    this. = clusterSpec;
    this. = cluster;
  }
  public void addRules(Rule... rulesthrows IOException {
    for (Rule rule : rules) {
      addRule(rule);
    }
  }
  public void addRules(Set<Rulerulesthrows IOException {
    for (Rule rule : rules) {
      addRule(rule);
    }
  }
  
  
Rules are additive. If no source is set then it will default to org.apache.whirr.ClusterSpec.getClientCidrs(), or, if that is not set, to the client's originating IP. If no destinations or ports are set then the rule has not effect.

Parameters:
rule The rule to add to the firewall.
Throws:
java.io.IOException
  public void addRule(Rule rulethrows IOException {
    Set<Instanceinstances = Sets.newHashSet();
    if (rule.destinations != null) {
      instances.addAll(rule.destinations);
    }
    if (rule.destinationPredicate != null) {
      instances.addAll(.getInstancesMatching(rule.destinationPredicate));
    }
    List<Stringcidrs;
    if (rule.source == null) {
      cidrs = .getClientCidrs();
      if (cidrs == null || cidrs.isEmpty()) {
        cidrs = Lists.newArrayList(FirewallSettings.getOriginatingIp());
      }
    } else {
      cidrs = Lists.newArrayList(rule.source + "/32");
    }
    .info("Authorizing firewall ingress to {} on ports {} for {}",
        new Object[] { instancesrule.portscidrs });
    FirewallSettings.authorizeIngress(instances,
        cidrsrule.ports);
  }
New to GrepCode? Check out our FAQ X