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

c7n-mailer: Can't subtract offset-naive and offset-aware datetimes #5468

Open
nitrocode opened this issue Mar 19, 2020 · 0 comments
Open

c7n-mailer: Can't subtract offset-naive and offset-aware datetimes #5468

nitrocode opened this issue Mar 19, 2020 · 0 comments

Comments

@nitrocode
Copy link
Contributor

nitrocode commented Mar 19, 2020

Describe the bug
When using the get_date_age jinja2 filter, I receive the error Can't subtract offset-naive and offset-aware datetimes

To Reproduce

  1. Create a policy
  2. Create a jinja template using get_date_age
  3. Run the policy to create the sqs message
  4. Run the mailer lambda to read the sqs message

The mailer should now show an exception

Expected behavior
A clear and concise description of what you expected to happen.

No errors

Background (please complete the following information):

  • OS: [e.g. OSX 10.15]
  • Python Version: python 3.8.1
  • Custodian Version: 0.8.46.1
  • Tool Version: c7n-mailer doesn't seem to have a --version switch...
  • Cloud Provider: aws
  • Policy:
vars:
  mode: &mode
    type: periodic
    schedule: "rate(7 days)"
    role: arn:aws:iam::{account_id}:role/cloud-custodian
    tags:
      application: cloud-custodian
      team: sre
      env: production

policies:
  - name: ami-unused
    description: |
      Unused AMIs older than a year
    resource: ami
    max-resources: 100
    mode:
      <<: *mode
    filters:
      - type: unused
        value: true
      - type: image-age
        days: 365
    actions:
      # - deregister
      - type: notify
        slack_template: ami-unused-slack
        to:
          - https://hooks.slack.com/
        transport:
          type: sqs
          queue: https://sqs.us-west-2.amazonaws.com/{account_id}/cloud-custodian
  • Template:
{
  "attachments": [
    {
      "fallback": "Cloud Custodian Policy Violation",
      "title": "{{ policy['name'] }}",
      "color": "warning",
      "fields": [
        {
          "title": "Account",
          "value": "{{ account }}"
        },
        {
          "title": "Region",
          "value": "{{ region }}"
        },
        {
          "title": "Description",
          "value": "{{ policy['description'].strip() }}"
        },
        {
          "title": "Found",
          "value": "{{ resources | length }}"
        },
        {
          "title": "Subset",
          "value": "{%- for resource in (resources|sort(attribute='CreationDate'))[:5] -%}
                      {{ resource['ImageId'] }}, {% set age = (resource['CreationDate'] | date_time_format(format='%Y %b %d %H:%M %Z')) | get_date_age %}{{'%0.2f' % (age / 365)|float}} years, (tags: 
                      {%- for tag in resource['Tags'] -%}
                        {{ tag['Key'] }}={{ tag['Value'] }},
                      {%- endfor -%})\n\n
                    {%- endfor -%}"
        }
      ]
    }
  ],
  {% if recipient is string and not recipient.startswith('https://') %}
  "channel":"{{ recipient }}",
  {% endif -%}
  "username": "Custodian"
}
  • Traceback:
  File "/var/task/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "msg-templates/ami-unused-slack.j2", line 32, in top-level template code
    {{ resource['ImageId'] }}, {% set age = (resource['CreationDate'] | date_time_format(format='%Y %b %d %H:%M %Z')) | get_date_age %}{{'%0.2f' % (age / 365)|float}} years, (tags:
  File "/var/task/c7n_mailer/utils.py", line 144, in get_date_age
    return (datetime.now(tz=tzutc()) - parser.parse(date)).days

Additional context
Add any other context about the problem here.

This works using replay.

c7n-mailer-replay \
    --config mailer.yml \
    -t templates \
    -p messages/sqs-ami-unused.slack.json

Possible fix for python2 or python3: https://stackoverflow.com/questions/796008/cant-subtract-offset-naive-and-offset-aware-datetimes

https://github.com/cloud-custodian/cloud-custodian/blob/master/tools/c7n_mailer/c7n_mailer/utils.py#L147

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants