Posts

Website SecurityTester

Mittwoch, 12. April 2017, 08:40 Uhr | roberto@vasquez-angel.de |
https://www.ssllabs.com/ssltest/analyze.html

Overwrite a git branch with another

Samstag, 01. April 2017, 13:29 Uhr | roberto@vasquez-angel.de |

Assume you have a branch_new branch and want to overwrite branch_old with its content:

git checkout branch_new
git merge -s ours branch_old
git checkout branch_old
git merge branch_new

Rails: Using dynamic "interpolations" in VCR

Mittwoch, 22. Februar 2017, 09:03 Uhr | roberto@vasquez-angel.de |

When using the vcr gem, you may encounter the problem, that you need to have variables in your requests. For example, your colleagues use different hostnames for local api endpoints. Then you have the problem, that a recorded cassette does not work everywhere.

VCR has a configuration option that is called define_cassette_placeholder. With this option you can define a string and a code block, that will be run replacing this string in your cassette.

Here is a simplified example:

# spec/support/vcr.rb
VCR.configure do |c|
  c.define_cassette_placeholder("<API_HOST>") do
    hosts = { 
      'default'     => 'my_app.ldev'
      'kara.thrace' => 'my_app.localdev',
      'bill.adama'  => 'my_app.dev'
    }
    user = ENV.fetch('USER') { default }
    hosts[user]
  end
end

# spec/fixtures/vcr_cassettes/example.yml
...
http_interactions:
- request:
    method: get
    uri: http://<API_HOST>/api/v1/cic/dradis_status.json

puma-dev with dev-tld-resolver on ubuntu 14.04 LTS

Mittwoch, 12. Oktober 2016, 22:42 Uhr | roberto@vasquez-angel.de |

Things to know:

The default tld .dev does not work anymore:

$ dig issue.dev TXT +short
"Your DNS configuration needs immediate attention see https://icann.org/namecollision"

You can restart servers via:

touch tmp/restart.txt

Don’t know how or if it is possible to handler apps with different ruby versions.

TODO:

  • bind to 80/443
  • publish puma-dev/dev-tld-resolver install scripts (create apt package?)

Find apt package by filename

Donnerstag, 22. September 2016, 12:23 Uhr | roberto@vasquez-angel.de |

Install apt-file:

sudo apt-get install apt-file
sudo apt-file update

Search for something:

 sudo apt-file find curl.h

Rails: Using factories from engines

Dienstag, 02. August 2016, 16:52 Uhr | roberto@vasquez-angel.de |

If you have engines, that - for your testing convenience - include factories, you may want to use them:

# spec/support/factory_girl_rails.rb:
require 'factory_girl_rails'

FactoryGirl.definition_file_paths << MultiClient::Engine.root.join(*%w(spec factories))
FactoryGirl.factories.clear
FactoryGirl.find_definitions

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end

Safer ruby on rails development

Dienstag, 22. März 2016, 16:49 Uhr | roberto@vasquez-angel.de |

To avoid forgetting translations or params, you can add following settings to your application. Then, missing translations or unpermitted params will raise an exception.

# config/environments/development.rb  
# Raises error for missing translations
config.action_view.raise_on_missing_translations = true

# Raises an error on unpermitted attributes assignment
config.action_controller.action_on_unpermitted_parameters = :raise

Adding API Header Authentication when using Authlogic

Donnerstag, 03. März 2016, 15:13 Uhr | roberto@vasquez-angel.de |

A simple way to add Header Authentication to Authlogic, is to use a before_action filter to extract the authentication information from the header and inject it to the params hash. So you can use the default single access token.

class BackendController < ApplicationController
  before_action :extract_authentication_from_header, if: -> { request.headers['Authorization'].present? }
  before_action :authenticate_user!

  private

  def single_access_allowed?
    true
  end

  # Extract authentication from request headers and inject it into params.
  # Accepted headers example:
  # 
  # Authorization: Token token="<SINGLE_ACCESS_TOKEM>"
  # 
  def extract_authentication_from_header
    authlogic_params_key = :user_api_key
    raw_header = request.headers['Authorization']
    auth_token = raw_header.split("=\"").last[0..-2]
    params[authlogic_params_key] = auth_token
  end
end

The structure of the header is very losely based on this document: https://tools.ietf.org/html/draft-hammer-http-token-auth-01#section-5.1

AVM Fritzbox VPN with DynDNS vs. myFritz (Error 0x1c)

Sonntag, 24. Januar 2016, 21:39 Uhr | roberto@vasquez-angel.de |

When using myFritz and dyndns it is mandatory, that you use the myFritz dns names as VPN addresses on both sides. It is not possible to use dyndns entries. If you use them anyways, you’ll get following error in the logs:

VPN-Fehler: xyz.dynvpn.de, IKE-Error 0x1c

The AVM docs (https://avm.de/service/fritzbox/fritzbox-7390/wissensdatenbank/publication/show/687_VPN-Verbindung-zwischen-zwei-FRITZ-Box-Netzwerken-kann-nicht-hergestellt-werden/) hide this pretty well in the last sentence of their vpn debugging faq:

Wichtig:Ist in einer FRITZ!Box sowohl MyFRITZ! als auch Dynamic DNS aktiv, muss bei der Einrichtung der VPN-Verbindung zwingend der MyFRITZ!-Domainname als "Internetadresse" eingetragen werden. Soll bei der VPN-Verbindung als "Internetadresse" eine feste ("statische") öffentliche IP-Adresse verwendet werden, darf in der FRITZ!Box weder MyFRITZ! noch Dynamic DNS aktiv sein.

Rails 4 Spec and Tool Stack

Mittwoch, 13. Januar 2016, 15:43 Uhr | roberto@vasquez-angel.de |
# Gemfile
gem 'capybara',           groups: [:test]
gem 'factory_girl_rails', groups: [:test]
gem 'rspec-rails',        groups: [:development, :test]
gem 'shoulda-matchers',   groups: [:test]
gem 'guard-bundler',      groups: [:development]
gem 'guard-rails',        groups: [:development]
gem 'guard-rspec',        groups: [:development]

gem 'rubocop'
gem 'yaml_db'

Route Translator

Gemfile

gem 'route_translator'

config/initializers/route_translator.rb

RouteTranslator.config do |config|
  config.force_locale = true
end

Simple Form

Gemfile

gem 'simple_form'

Commandline: rails generate simple_form:install –bootstrap

Resizing Virtualbox disks

Sonntag, 27. Dezember 2015, 23:50 Uhr | roberto@vasquez-angel.de |

Clones and resizes source.vmdk to resized.vmdk (64GB):

VBoxManage clonehd "source.vmdk" "cloned.vdi" --format vdi
VBoxManage modifyhd "cloned.vdi" --resize 65536
move source.vmdkk source.vmdk.original
VBoxManage clonehd "cloned.vdi" "source.vmdk" --format vmdk
VBoxManage.exe internalcommands sethduuid source.vmdk

Then replace all occurences of the old UUID with the new UUID in your virtual box xml file. The extra space can now be allocated with partitioning tools like gparted, etc.

Rails, Active Admin, Rspec: Fixing uninitialized constant Admin::DashboardController with

Dienstag, 08. Dezember 2015, 09:38 Uhr | roberto@vasquez-angel.de |

when trying to test an active admin backend with Rspec, you can stumble upon following error:

  ActionController::RoutingError:
    uninitialized constant Admin::DashboardController

If this happens, append following lines to your spec/rails_helper.rb:

# spec/rails_helper.rb
require_relative 'dummy/app/admin/dashboard'
require_relative 'dummy/config/routes'

Beautiful button group toggles with rails, bootstrap 3 and haml

Donnerstag, 03. Dezember 2015, 08:19 Uhr | roberto@vasquez-angel.de |
.btn-group{ role: :group }
  - if item.published?
    %button.btn.btn-secondary.btn-xs.disabled{ type: :button }
      %span.glyphicon.glyphicon-eye-open
    %button.btn.btn-xs.btn-disabled
      %span.glyphicon.glyphicon-ok
    = link_to(toggle_published_item_path(item), class: 'btn btn-xs btn-danger', method: :post, title: t('.unpublish')) do
      %span.glyphicon.glyphicon-eye-close
  - else
    = link_to(toggle_published_item_path(item), class: 'btn btn-xs btn-success', method: :post, title: t('.publish')) do
      %span.glyphicon.glyphicon-eye-open
    %button.btn.btn-xs.btn-disabled
      %span.glyphicon.glyphicon-remove
    %button.btn.btn-secondary.btn-xs.disabled{ type: :button }
      %span.glyphicon.glyphicon-eye-close

Rails: Fixing uninitialized constant Admin::DashboardController when testing with rspec

Samstag, 21. November 2015, 13:19 Uhr | roberto@vasquez-angel.de |

When trying to test active admin with rails and rspec and your get following error:

ActionController::RoutingError:
        uninitialized constant Admin::DashboardController

Try adding following lines to your rails_helper:

require_relative 'dummy/app/admin/dashboard'
require_relative 'dummy/config/routes'

Time based routing for Rails

Dienstag, 17. November 2015, 10:30 Uhr | roberto@vasquez-angel.de |

If you want to add routes in rails, based on a timeframe, you can do this as follows:

In config/routes.rb

  scope constraints: lambda { |request| TimeRoutingConstraint.new('18.11.2015 18:00', '17.12.2015 00:00').matches?(request) } do
    get 'gewinnspiel', to: 'contest/registrations#new',
                       as: 'new_contest_registration'

    mount Contest::Engine, at: '/contest'
  end
  get 'gewinnspiel', to: 'contest/registrations#closed'

In app/routing_constraints/time_routing_constraint.rb

class TimeRoutingConstraint
  def initialize(from, to)
    @from = Time.zone.parse(from)
    @to   = Time.zone.parse(to)
  end

  def matches?(request)
    now = Time.zone.now
    if now > @from && now < @to
      true
    else
      false
    end
  end
end

Amazon S3 Commandline

Freitag, 13. November 2015, 10:16 Uhr | roberto@vasquez-angel.de |

Set credentials:

aws configure

Show bucket files:

aws s3 ls s3://<bucket_name>

Get bucket region:

aws s3api get-bucket-location --bucket <bucket_name>

Upload folder to bucket:

aws s3 cp <local_folder> s3://<bucket_name> --recursive

Same with region:

aws s3 cp <local_folder> s3://<bucket_name> --recursive --region <region>

Hint: Frankfurt is eu-central-1

Docker basics

Mittwoch, 11. November 2015, 08:43 Uhr | roberto@vasquez-angel.de |

Build docker image from Dockerfile

docker build .

Tag image:

docker tag <hash> <organisation>/<image_name>:<version>

Push to dockerhub:

docker push <organisation>/<image_name>

List local docker images

docker images

Delete docker tag:

docker rmi <hash>

List running images/processes:

docker ps -l

Stop container:

docker stop <container_idsh>

Start container

docker start <hash>

Run an image and bash into it:

docker run -t -i <hash/image_name:tag> /bin/bash

Run a container with the current directory mounted int /app:

docker run -v $PWD:/app -it <hash/image_name:tag> /bin/bash

Docker and Rails

Bundle and run Rspec suite:

docker run --volume $PWD:/data --workdir /data -ti example/rails:2.0.2 /bin/bash -lc 'cd . && bundle install && bundle exec rspec'

Find all german yaml in a rails project files and zip them

Mittwoch, 08. Juli 2015, 14:43 Uhr | roberto@vasquez-angel.de |
find . -wholename "*/locales/*de.yml" -print | zip locale_files-2015-07-08.zip -@

Autloading for the lib-folder inside rails engines

Freitag, 26. Juni 2015, 08:37 Uhr | roberto@vasquez-angel.de |
# foo/engine.rb
module Foo
  class Engine < ::Rails::Engine
    config.autoload_paths += Dir["#{config.root}/lib/**/"]
  end
end

Adding SSH Key to remote server

Sonntag, 15. März 2015, 12:49 Uhr | roberto@vasquez-angel.de |
$ ssh-copy-id -i username@remote-host

Expired users in oracle

Montag, 08. Dezember 2014, 10:21 Uhr | roberto@vasquez-angel.de |

To view a list of users and their status use following SQL statement:

select username,expiry_date,account_status from dba_users;

To unlock a user:

alter user <USERNAME> identified by <PASSWORD> account unlock;

To unlock an expired system user on oracle 12c, you habe to specify the container:

alter session set container=CDB$ROOT;
alter user system identified by oracle container=all;

Otherwise you’ll get following error:

ORA-65066: The specified changed must apply to all containers

SQL to see connections in oracle

Freitag, 21. November 2014, 10:30 Uhr | roberto@vasquez-angel.de |
SELECT username,
       osuser,
       terminal
FROM v$session
WHERE username IS NOT NULL
ORDER BY username ASC;

SQL to check tempspace in oracle

Donnerstag, 20. November 2014, 16:41 Uhr | roberto@vasquez-angel.de |
SELECT b.Total_MB,
       b.Total_MB - round(a.used_blocks*8/1024) Current_Free_MB,
       round(used_blocks*8/1024) Current_Used_MB,
       round (max_used_blocks*8/1024) Max_used_MB
FROM v$sort_segment a,
  (SELECT round(sum(bytes)/1024/1024) Total_MB
   FROM dba_temp_files) b;

Most common mistakes on legacy rails apps

Freitag, 18. Juli 2014, 07:45 Uhr | roberto@vasquez-angel.de |

Good blog post!

Most common mistakes on legacy rails apps

Ruby on Rails: Get GC info with rack-mini-profiler

Freitag, 13. Juni 2014, 13:44 Uhr | roberto@vasquez-angel.de |

Append ‘?pp=profile-gc’ to the url