saltstack/salt

View on GitHub
salt/modules/iptables.py

Summary

Maintainability
F
1 wk
Test Coverage

File iptables.py has 1234 lines of code (exceeds 250 allowed). Consider refactoring.
Open

# -*- coding: utf-8 -*-
'''
Support for iptables

Configuration Options
Severity: Major
Found in salt/modules/iptables.py - About 3 days to fix

    Function _parser has 384 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    def _parser():
        '''
        This function attempts to list all the options documented in the
        iptables(8) and iptables-extensions(8) man pages.  They will not all be
        used by all parts of the module; use them intelligently and appropriately.
    Severity: Major
    Found in salt/modules/iptables.py - About 1 day to fix

      Function build_rule has a Cognitive Complexity of 90 (exceeds 5 allowed). Consider refactoring.
      Open

      def build_rule(table='filter', chain=None, command=None, position='', full=None, family='ipv4',
                     **kwargs):
          '''
          Build a well-formatted iptables rule based on kwargs. A `table` and `chain`
          are not required, unless `full` is True.
      Severity: Minor
      Found in salt/modules/iptables.py - About 1 day to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Cyclomatic complexity is too high in function build_rule. (57)
      Open

      def build_rule(table='filter', chain=None, command=None, position='', full=None, family='ipv4',
                     **kwargs):
          '''
          Build a well-formatted iptables rule based on kwargs. A `table` and `chain`
          are not required, unless `full` is True.
      Severity: Minor
      Found in salt/modules/iptables.py by radon

      Cyclomatic Complexity

      Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

      Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

      Construct Effect on CC Reasoning
      if +1 An if statement is a single decision.
      elif +1 The elif statement adds another decision.
      else +0 The else statement does not cause a new decision. The decision is at the if.
      for +1 There is a decision at the start of the loop.
      while +1 There is a decision at the while statement.
      except +1 Each except branch adds a new conditional path of execution.
      finally +0 The finally block is unconditionally executed.
      with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
      assert +1 The assert statement internally roughly equals a conditional statement.
      Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
      Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

      Source: http://radon.readthedocs.org/en/latest/intro.html

      Function _parse_conf has a Cognitive Complexity of 48 (exceeds 5 allowed). Consider refactoring.
      Open

      def _parse_conf(conf_file=None, in_mem=False, family='ipv4'):
          '''
          If a file is not passed in, and the correct one for this OS is not
          detected, return False
          '''
      Severity: Minor
      Found in salt/modules/iptables.py - About 7 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function _conf has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring.
      Open

      def _conf(family='ipv4'):
          '''
          Some distros have a specific location for config files
          '''
          if __grains__['os_family'] == 'RedHat':
      Severity: Minor
      Found in salt/modules/iptables.py - About 3 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Cyclomatic complexity is too high in function _parse_conf. (26)
      Open

      def _parse_conf(conf_file=None, in_mem=False, family='ipv4'):
          '''
          If a file is not passed in, and the correct one for this OS is not
          detected, return False
          '''
      Severity: Minor
      Found in salt/modules/iptables.py by radon

      Cyclomatic Complexity

      Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

      Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

      Construct Effect on CC Reasoning
      if +1 An if statement is a single decision.
      elif +1 The elif statement adds another decision.
      else +0 The else statement does not cause a new decision. The decision is at the if.
      for +1 There is a decision at the start of the loop.
      while +1 There is a decision at the while statement.
      except +1 Each except branch adds a new conditional path of execution.
      finally +0 The finally block is unconditionally executed.
      with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
      assert +1 The assert statement internally roughly equals a conditional statement.
      Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
      Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

      Source: http://radon.readthedocs.org/en/latest/intro.html

      Function check has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
      Open

      def check(table='filter', chain=None, rule=None, family='ipv4'):
          '''
          Check for the existence of a rule in the table and chain
      
          This function accepts a rule in a standard iptables command format,
      Severity: Minor
      Found in salt/modules/iptables.py - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function insert has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
      Open

      def insert(table='filter', chain=None, position=None, rule=None, family='ipv4'):
          '''
          Insert a rule into the specified table/chain, at the specified position.
      
          This function accepts a rule in a standard iptables command format,
      Severity: Minor
      Found in salt/modules/iptables.py - About 55 mins to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function _regex_iptables_save has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
      Open

      def _regex_iptables_save(cmd_output, filters=None):
          '''
          Return string with `save_filter` regex entries removed.  For example:
      
          If `filters` is not provided, it will be pulled from minion config,
      Severity: Minor
      Found in salt/modules/iptables.py - About 45 mins to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Avoid deeply nested control flow statements.
      Open

                          if args[index].startswith('-') or (args[index] == '!' and
                                                             not swap):
                              args.insert(index, '')
                          else:
                              while (index + 1 < len(args) and
      Severity: Major
      Found in salt/modules/iptables.py - About 45 mins to fix

        Avoid too many return statements within this function.
        Open

                    return '/etc/iptables/iptables.rules'
        Severity: Major
        Found in salt/modules/iptables.py - About 30 mins to fix

          Avoid too many return statements within this function.
          Open

                      return '/var/lib/ip6tables/rules-save'
          Severity: Major
          Found in salt/modules/iptables.py - About 30 mins to fix

            Avoid too many return statements within this function.
            Open

                return ' '.join(rule)
            Severity: Major
            Found in salt/modules/iptables.py - About 30 mins to fix

              Avoid too many return statements within this function.
              Open

                      return True
              Severity: Major
              Found in salt/modules/iptables.py - About 30 mins to fix

                Avoid too many return statements within this function.
                Open

                            return '/etc/iptables/rules.v4'
                Severity: Major
                Found in salt/modules/iptables.py - About 30 mins to fix

                  Avoid too many return statements within this function.
                  Open

                          return '/etc/sysconfig/scripts/SuSEfirewall2-custom'
                  Severity: Major
                  Found in salt/modules/iptables.py - About 30 mins to fix

                    Avoid too many return statements within this function.
                    Open

                                return '/etc/iptables/rules-save'
                    Severity: Major
                    Found in salt/modules/iptables.py - About 30 mins to fix

                      Avoid too many return statements within this function.
                      Open

                              return '{0} {1} -t {2} {3}{4} {5} {6} {7}'.format(_iptables_cmd(family),
                      Severity: Major
                      Found in salt/modules/iptables.py - About 30 mins to fix

                        Avoid too many return statements within this function.
                        Open

                                    return '/etc/iptables/ip6tables.rules'
                        Severity: Major
                        Found in salt/modules/iptables.py - About 30 mins to fix

                          Avoid too many return statements within this function.
                          Open

                              return out
                          Severity: Major
                          Found in salt/modules/iptables.py - About 30 mins to fix

                            Avoid too many return statements within this function.
                            Open

                                        return '/etc/iptables/rules6-save'
                            Severity: Major
                            Found in salt/modules/iptables.py - About 30 mins to fix

                              Avoid too many return statements within this function.
                              Open

                                  return out
                              Severity: Major
                              Found in salt/modules/iptables.py - About 30 mins to fix

                                Avoid too many return statements within this function.
                                Open

                                            return '/var/lib/iptables/rules-save'
                                Severity: Major
                                Found in salt/modules/iptables.py - About 30 mins to fix

                                  Avoid too many return statements within this function.
                                  Open

                                              return '/etc/iptables/rules.v6'
                                  Severity: Major
                                  Found in salt/modules/iptables.py - About 30 mins to fix

                                    Similar blocks of code found in 2 locations. Consider refactoring.
                                    Open

                                        after_jump_arguments = (
                                            'j',  # j and jump needs to be first
                                            'jump',
                                    
                                            # IPTABLES
                                    Severity: Major
                                    Found in salt/modules/iptables.py and 1 other location - About 7 hrs to fix
                                    salt/modules/namecheap_domains.py on lines 149..156

                                    Duplicated Code

                                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                    Tuning

                                    This issue has a mass of 118.

                                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                    Refactorings

                                    Further Reading

                                    Similar blocks of code found in 2 locations. Consider refactoring.
                                    Open

                                    def delete_chain(table='filter', chain=None, family='ipv4'):
                                        '''
                                        .. versionadded:: 2014.1.0
                                    
                                        Delete custom chain to the specified table.
                                    Severity: Major
                                    Found in salt/modules/iptables.py and 1 other location - About 4 hrs to fix
                                    salt/modules/iptables.py on lines 774..800

                                    Duplicated Code

                                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                    Tuning

                                    This issue has a mass of 91.

                                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                    Refactorings

                                    Further Reading

                                    Similar blocks of code found in 2 locations. Consider refactoring.
                                    Open

                                    def new_chain(table='filter', chain=None, family='ipv4'):
                                        '''
                                        .. versionadded:: 2014.1.0
                                    
                                        Create new custom chain to the specified table.
                                    Severity: Major
                                    Found in salt/modules/iptables.py and 1 other location - About 4 hrs to fix
                                    salt/modules/iptables.py on lines 803..829

                                    Duplicated Code

                                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                    Tuning

                                    This issue has a mass of 91.

                                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                    Refactorings

                                    Further Reading

                                    Similar blocks of code found in 4 locations. Consider refactoring.
                                    Open

                                        if 'dport' in kwargs:
                                            rule.append('{0}--dport {1}'.format(maybe_add_negation('dport'), kwargs['dport']))
                                            del kwargs['dport']
                                    Severity: Major
                                    Found in salt/modules/iptables.py and 3 other locations - About 55 mins to fix
                                    salt/modules/iptables.py on lines 282..284
                                    salt/modules/iptables.py on lines 286..288
                                    salt/modules/iptables.py on lines 337..339

                                    Duplicated Code

                                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                    Tuning

                                    This issue has a mass of 45.

                                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                    Refactorings

                                    Further Reading

                                    Similar blocks of code found in 4 locations. Consider refactoring.
                                    Open

                                        if 'sport' in kwargs:
                                            rule.append('{0}--sport {1}'.format(maybe_add_negation('sport'), kwargs['sport']))
                                            del kwargs['sport']
                                    Severity: Major
                                    Found in salt/modules/iptables.py and 3 other locations - About 55 mins to fix
                                    salt/modules/iptables.py on lines 282..284
                                    salt/modules/iptables.py on lines 286..288
                                    salt/modules/iptables.py on lines 333..335

                                    Duplicated Code

                                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                    Tuning

                                    This issue has a mass of 45.

                                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                    Refactorings

                                    Further Reading

                                    Similar blocks of code found in 4 locations. Consider refactoring.
                                    Open

                                        if 'of' in kwargs:
                                            rule.append('{0}-o {1}'.format(maybe_add_negation('of'), kwargs['of']))
                                            del kwargs['of']
                                    Severity: Major
                                    Found in salt/modules/iptables.py and 3 other locations - About 55 mins to fix
                                    salt/modules/iptables.py on lines 282..284
                                    salt/modules/iptables.py on lines 333..335
                                    salt/modules/iptables.py on lines 337..339

                                    Duplicated Code

                                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                    Tuning

                                    This issue has a mass of 45.

                                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                    Refactorings

                                    Further Reading

                                    Similar blocks of code found in 4 locations. Consider refactoring.
                                    Open

                                        if 'if' in kwargs:
                                            rule.append('{0}-i {1}'.format(maybe_add_negation('if'), kwargs['if']))
                                            del kwargs['if']
                                    Severity: Major
                                    Found in salt/modules/iptables.py and 3 other locations - About 55 mins to fix
                                    salt/modules/iptables.py on lines 286..288
                                    salt/modules/iptables.py on lines 333..335
                                    salt/modules/iptables.py on lines 337..339

                                    Duplicated Code

                                    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                    Tuning

                                    This issue has a mass of 45.

                                    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                    Refactorings

                                    Further Reading

                                    There are no issues that match your filters.

                                    Category
                                    Status