{ Josh Rendek }

<3 Go & Kubernetes · honeypots · homelab · leadership

Jun 17, 2015 · 1 min

Faster docker builds using a cache

If you’re using bundler for your ruby or rails project and docker you will run into docker having to install your gems everytime. You can either make a base image that has the bundle cache already on it, or you can make a small cache step in your Dockerfile.

Here I’ve setup a cache user and host to store the cache tar. It will attempt to download and untar it, run bundle, then attempt to tar and re-upload it.

read more

Feb 21, 2015 · 4 min

Communication between React components using events

Here is an example of a clean way to communicate between React components without getting stuck passing @prop callbacks all around. Inspired by looking at the new Flux React utilities.

We’re going to start off with a simple HAML file:

1%script{src: "https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react-with-addons.js"}
2%div{data: { ui: 'alerts' }}
3%div{data: { ui: 'widgets' }}
4:javascript
5  React.renderComponent(Widgets(), document.querySelector('[data-ui="widgets"]'))
6  React.renderComponent(Alerts(), document.querySelector('[data-ui="alerts"]'))

Next comes our Widget component.

1{div, button} = React.DOM
2
3Widgets = React.createClass
4	render: ->
5    div className: 'widget',
6    	button className: 'btn btn-primary', onClick: (=> @_sendMsg('Testing')), 'Click Me'
7	_sendMsg: (msg) ->
8    $('[data-ui="alerts"]').trigger("message", ["Widget clicked."])

On line 1 we’re defining some easy helper methods to access the React.DOM object - otherwise on every line we’d be writing something like React.DOM.div or whichever element we were going to call.

read more

Aug 22, 2014 · 1 min

Rails force HTTPS for your application

This is your setup:

  • Nginx redirecting HTTP to HTTPS
  • Unicorn/puma/(etc) being reverse proxied to via nginx

What you may not realize:

When you’re submitting a form and using the redirect_to or respond_with @object, location: [] methods rails may not pick up that you want to use HTTPS as the protocol, adding this to your application_controller.rb ensures every URL generated by rails will go to a secure protocol:

1def default_url_options(options={})
2    options.merge!(protocol: 'https') unless Rails.env.development? || Rails.env.test?
3    options
4end

I ran into issues with rails redirecting to plain HTTP when doing some uploading utilizing iframes (and Chrome rightfully blocking the redirect back to http). Hope this helps someone else! No need to use any special gems.

read more

Jul 8, 2014 · 2 min

Go-lang compare *ssh.Request.Type against a string

I was working on the agent for SSH Pot and ran into something interesting last night. A lot of the brute force attempts attempt to run a command like this:

1ssh user@host 'uname'

This is different than:

1ssh user@host
2$ uname

The first command is executing a command then exiting, the second is actually logging in and giving the user a shell. The first requests a exec subsystem and the second requests a shell subsystem - so there are two ways to handle it.

read more

Jun 18, 2014 · 2 min

Go-lang: mocking exec.Command using interfaces

This is a short example showing how to use an interface to ease testing, and how to use an interface with running shell commands / other programs and providing mock output.

Source on Github

Here is our main file that actually runs the commands and prints out “hello”.

 1package main
 2
 3import (
 4	"fmt"
 5	"os/exec"
 6)
 7
 8// first argument is the command, like cat or echo,
 9// the second is the list of args to pass to it
10type Runner interface {
11	Run(string, ...string) ([]byte, error)
12}
13
14type RealRunner struct{}
15
16var runner Runner
17
18// the real runner for the actual program, actually execs the command
19func (r RealRunner) Run(command string, args ...string) ([]byte, error) {
20	out, err := exec.Command(command, args...).CombinedOutput()
21	return out, err
22}
23
24func Hello() string {
25	out, err := runner.Run("echo", "hello")
26	if err != nil {
27		panic(err)
28	}
29	return string(out)
30}
31
32func main() {
33	runner = RealRunner{}
34	fmt.Println(Hello())
35}

Here is our test file. We start by defining our TestRunner type and implementing the Run(...) interface for it.

read more

Jun 9, 2014 · 1 min

A useful logger in Go

Small function that will print out useful information when invoked:

 1func logMsg(msg string) {
 2	pc, _, _, _ := runtime.Caller(1)
 3	caller := runtime.FuncForPC(pc).Name()
 4	_, file, line, _ := runtime.Caller(0)
 5	sp := strings.Split(file, "/")
 6	short_path := sp[len(sp)-2 : len(sp)]
 7	path_line := fmt.Sprintf("[%s/%s:%d]", short_path[0], short_path[1], line)
 8	log_string := fmt.Sprintf("[%s]%s %s:: %s", time.Now(), path_line, caller, msg)
 9	fmt.Println(log_string)
10}

Sample output:

read more

Apr 26, 2014 · 6 min

Motivation to work on new projects

Whenever I have spare time ( often around Christmas or when I’m on vacation/traveling ), I tend to fill it with working on projects I’ve built up in my backlog. I’m also really trying to keep a continuous streak of OSS commits going on Github (something about filling that chart up makes me want to work harder). Here’s my process and how I go about working on personal projects and try to stay motivated - if you have any ideas I’d love to hear them in the comments!

read more

Nov 15, 2013 · 3 min

2 patterns for refactoring with your ruby application

When working on a rails application you can sometimes find duplicated or very similar code between two different controllers (for instance a UI element and an API endpoint). Realizing that you have this duplication there are several things you can do. I’m going to go over how to extract this code out into the query object pattern 1 and clean up our constructor using the builder pattern 2 adapted to ruby.

read more

Oct 31, 2013 · 2 min

Parsing HTML in Scala

Is there ever a confusing amount of information out there on parsing HTML in Scala. Here is the list of possible ways I ran across:

  • Hope the document is valid XHTML and use scala.xml.XML to parse it
  • If the document isn’t valid XHTML use something like TagSoup and hope it parses again
  • Still think its valid XHTML? Try using scalaz’s XML parser

All of the answers I found on Google pointed to some type of XML parsing, which won’t always work. Coming from Ruby I know there are tools out there like Selenium that can simulate a web browser for you and give you a rich interface to interact with the returned HTML.

read more