How does one script the Keychain via rb-appscript? - ruby

I followed the docs and wrote:
require 'rubygems'
require 'appscript'
loginKeychain = Appscript::app('Keychain Scripting').keychains['login.keychain']
userName = loginKeychain.keys[its.name.eq(name)].password.get
UPDATE: if I run the script via Terminal, it succeeds the first time, and then returns the error below until I quit and restart Terminal; and repeat...
Error returned intermittently:
/Library/Ruby/Gems/1.8/gems/rb-appscript-0.5.3/lib/appscript.rb:539:in `_send_command': CommandError (Appscript::CommandError)
OSERROR: -600
MESSAGE: Application isn't running.
COMMAND: app("/System/Library/ScriptingAdditions/Keychain Scripting.app").keychains["login.keychain"].keys[its.name.eq("a name")].password.get()
UPDATE 2: Due to the mysterious error, and that Keychain Scripting seems buggy in general, I decided to use MacRuby. I found the MacRuby Keychain Wrapper library, which calls Keychain Services directly, rather than going through Scripting:
require 'keychain-wrapper/keychain'
MRKeychain::GenericItem.item_for_service(service_name).password
Easy
More efficient
Works flawlessly
Done.

I didn't use Ruby but I used Python and I have no trouble. I'd try doing regular Applescript and seeing if it can access Keychain Scripting. If not then try rebooting your Mac. But this sounds like more a problem of your computer than Appscript.

I'm getting that same error, but just with things that require authentification:
kc = app("Keychain Scripting").keychains["login.keychain"]
p kc.keys[its.name.eq("loginwindow")].name.get
# ["loginwindow"]
p kc.keys[its.name.eq("loginwindow")].password.get
# ... Application isn't running ...
tell app "Keychain Scripting" to password of keys of keychain "login.keychain" where name is "loginwindow" works just fine.

Related

Is it possible to automate logging into roblox?

I have been playing roblox a lot lately, and I was wondering if there was a way to automate the login process since I play on different computers each time. I looked into Python's selenium, and derived the script:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.roblox.com/login")
usernameStr = '<redacted>'
passwordStr = '<redacted>'
username = driver.find_element_by_id('login-username')
username.send_keys(usernameStr)
password = driver.find_element_by_id('login-password')
password.send_keys(passwordStr)
signInButton = driver.find_element_by_id('login-button')
signInButton.click()
When I run it in VScode it works perfectly, but when I exported the python file and tried running it via interpreter, it gave many errors around version types. I installed selenium on the computer as well. My hypothesis is my local computer had some tools in the background that when I tried on other computers didn't. Is there something that I am missing when going to my other computer? I am using python3.8.
One thing you could is make a script, when executed runs by the python in your $PATH.
#!/usr/bin/env python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.roblox.com/login")
usernameStr = '<redacted>'
passwordStr = '<redacted>'
username = driver.find_element_by_id('login-username')
username.send_keys(usernameStr)
password = driver.find_element_by_id('login-password')
password.send_keys(passwordStr)
signInButton = driver.find_element_by_id('login-button')
signInButton.click()
If you save this file in your terminal and run chmod +x <filename> to give it executable permissions, it will be a executable, which you can move from machines. Make sure to have all other selenium dependencies installed as well. When working with selenium, make sure your chrome driver is the same as your chrome browser, which you can read about on the chrome webdriver page.

Can't create bucket using aws-sdk ruby gem. Aws::S3::Errors::SignatureDoesNotMatch

I have a new computer and I'm trying to set up my AWS CLI environment so that I can run a management console I've created.
This is the code I'm running:
def create_bucket(bucket_args)
AWS_S3 = Aws::S3::Client.new(signature_version: 'v4')
AWS_S3.create_bucket(bucket_args)
end
Which raises this error:
Aws::S3::Errors::SignatureDoesNotMatch - The request signature we calculated does not match the signature you provided. Check your key and signing method.:
This was working properly on my other computer, which I no longer have access to. I remember debugging this same error on the other computer, and I thought I had resolved it by adding signature_version = s3v4 to my ~/.aws/config file. But this fix is not working on my new computer, and I'm not sure why.
To give some more context: I am using aws-sdk (2.5.5) and these aws cli specs: aws-cli/1.11.2 Python/2.7.12 Linux/4.4.0-38-generic botocore/1.4.60
In this case the issue was that my aws credentials (in ~/.aws/credentials) - specifically my secret token - were invalid.
The original had a slash in it:
xx/xxxxxxxxxxxxxxxxxxxxxxxxxx
which I didn't notice at first, so when I double clicked the token to select the word, it didn't include the first three characters. I then pasted this into the terminal when running aws configure.
To fix this, I found the correct, original secret acceess token and set the correct value in ~/.aws/credentials.

Run command after gem install from gem root folder

I'm deploying a Sinatra app as a gem. I have a command that starts the app as a service.
We are using chef to manage our deployments.
How can I run the command to start the app service but only after it's fully installed (including run-time dependencies)?
I've tried Googling for trying to run a post-install script but I haven't found anything that is of use or concrete without some complicated 'extconf.rb' work around
I would prefer not to use an execute resource if I can help it.
EDIT: I tried what was suggested but it breaks thins in way that causes berkshelf not to work in our pipeline.
Here's the code I'm using:
execute "run-service:post_install" do
cwd (f = File.expand_path(__FILE__).split('/')).shift(f.length - 3).join('\\')
timeout 5
command "bundle && rake service:post_install"
# action :nothing
# subscribes :run, "gem_package[gem_name]" , :delayed
end
It doesn't matter if I un-comment or not the last two lines, it just breaks things but if i take out the whole thing it stops breaking things. Obviously I'm doing something wrong but I'm not sure what.
EDIT:
IT's the command itself that breaks it, when I change command to ls and action to :run, it breaks.
EDIT:after changing the command path around a bit I managed to get it to spit out a usable error, it was trying to run the command from chef cook books path, so I've (hopefully) forced it to use the correct path.
Why do you not want to use an execute resource? That is exactly what it is for, running commands from Chef. Chef obeys the order of the resources, so if you have a gem_package followed by an execute they will run in that order.
So, In the end I decided to try using the service resource because it allows you to set start, and stop commands.
The code that I used is :
service service_name do
init_command ("#{%x(gem env gemdir).strip.gsub('/','\\')}\\gems\\gem_name-#{installing_version}")
start_command "rake service:start"
stop_command "rake service:stop"
reload_command "rake service:reload"
restart_command "rake service:restart"
supports start: true, restart: true, reload: true
action [:enable,:start]
end
I'm still having problems but this is of a different sort.

Ruby, random failures with File.new

I have a script that works great, except that it randomly fails generating new files...
this is the code:
...
file_log_path = File.join(Rails.root, 'log', "xls_import_#{Time.now.to_i}.log")
#log = File.new(file_log_path, 'w+')
....
and this is the error inside delayed_job.log
2012-12-21T18:18:41+0100: [Worker(delayed_job host:webserver2.netbanana.it pid:24482)] LoadDataFromCsv failed with Errno::ENOENT: No such file or directory - /var/www/rails/myapp/releases/20121210093945/log/xls_import_1356110321.log - 0 failed attempts
2012-12-21T18:18:41+0100: [Worker(delayed_job host:webserver2.netbanana.it pid:24482)] PERMANENTLY removing LoadDataFromCsv because of 1 consecutive failures.
Other times, it works! Someone can help me?
-- edit:
Well... it seems that Rails.root uses a wrong deploy path... in fact /var/www/rails/myapp/releases/20121210093945 doesn't exists.
But, as I said, the script sometimes works, sometimes not... If I reload delayed_job, my script works a few, and then start failing.
If you're using Capistrano to manage your releases, which I'm guessing is the case based on the path structure, then you'll need to be careful about referencing paths which can be removed after a deployment has occurred. DelayedJob needs to be restarted each time you deploy or it might be working in an orphaned directory.
If possible, you might want to use the shared/log path instead since that persists between deployments.
I found several delayed_job processes (zombies), still running... killed them all (not metallica's song) and now it works!

Path Error running CakePHP's "cake" console utility in Windows

A while ago when I started with Cake, I managed to get the console running in my Windows environment, and I have no idea how. I'm now having problems to make it work again in another computer.
This is what I've done in the new machine:
Downloaded my Cake code from source control (so all the files are exactly the same as the computer where it worked, including the configuration files)
Added PHP and "c:\my_cake_path\cake\console" to the path
If I run "cake OneOfMyShells", either standing on the /app, or in /cake/console directories, I get the following error:
Warning: get_object_vars() expects parameter 1 to be object, null given in C:\my_cake_path\cake\libs\model\connection_manager.php on line 199
Error: Missing Database Connection. Try 'cake bake'
"cake Bake", if run normally, when I try to get it to do the DB config ends up throwing another error (which is not that relevant to this)
However, if I run: cake bake -app "c:\my_cake_path\app"
Then bake works, I can do the database config, and it writes the DB config file (which is useless at this point, since I already had one)
Then, of course: cake OneOfMyShells -app "c:\my_cake_path\app"
does work perfectly well.
So, everything's working fine, I just need to manually specify the path to "app" every single time, which is very annoying.
How can I get around this? Where is Cake looking for to find the path to app?
Thanks!
Daniel
Not much of a difference in the sense you still need to type but you can run cake from the app dir like this: C:\XXX\project\app> ..\cake\console\cake.bat
To make it smaller you can put that line on a .bat or just add the \cake\console dir the windows path

Resources