Derby.js with Derby Auth errors - coffeescript

I just start with Derby and started out with integrating MongoDB and derby-auth. However, I am having troubles with that and can't figure it out. The following code returns the stacktrace below it. Finding out what causes the error is really hard. This is a new project generated with derby -c new project. All I know is that it's related to the auth.store(store) line.
UPDATE: I am also having this problem with the example/ in derby-auth, but only on my first request so I guess it would be relate to the database connection.
http = require 'http'
path = require 'path'
express = require 'express'
gzippo = require 'gzippo'
derby = require 'derby'
app = require '../app'
serverError = require './serverError'
auth = require('derby-auth')
MongoStore = require('connect-mongo')(express)
## SERVER CONFIGURATION ##
expressApp = express()
server = http.createServer expressApp
derby.use(require 'racer-db-mongo')
derby.use(derby.logPlugin)
store = derby.createStore
listen: server
db:
type: 'Mongo'
uri: 'mongodb://localhost/admin'
module.exports = server
auth.store(store)
ONE_YEAR = 1000 * 60 * 60 * 24 * 365
root = path.dirname path.dirname __dirname
publicPath = path.join root, 'public'
# Authentication strategies (providers actually)
strategies =
facebook:
strategy: require('passport-facebook').Strategy
conf:
clientID: 'boo'
clientSecret: 'boo'
# Authentication optionss
options =
domain: (if process.env.NODE_ENV is 'production' then "http://www.mydomain.com" else "http://localhost:3000" )
expressApp
.use(express.favicon())
# Gzip static files and serve from memory
.use(gzippo.staticGzip publicPath, maxAge: ONE_YEAR)
# Gzip dynamically rendered content
.use(express.compress())
# Uncomment to add form data parsing support
.use(express.bodyParser())
.use(express.methodOverride())
# Uncomment and supply secret to add Derby session handling
# Derby session middleware creates req.session and socket.io sessions
.use(express.cookieParser())
.use(store.sessionMiddleware
secret: 'mooo'
cookie: {maxAge: ONE_YEAR}
store: new MongoStore({ url: 'mongodb://localhost/admin' })
)
# Adds req.getModel method
.use(store.modelMiddleware())
# Authentication
.use(auth.middleware(strategies, options))
# Creates an express middleware from the app's routes
.use(app.router())
.use(expressApp.router)
.use(serverError root)
## SERVER ONLY ROUTES ##
expressApp.all '*', (req) ->
throw "404: #{req.url}"
Stack Trace:
TypeError: Cannot read property '_at' of undefined
at Model.mixin.proto._createRef ($derbypath/node_modules/derby/node_modules/racer/lib/refs/index.js:158:20)
at Model.mixin.proto.ref ($derbypath/node_modules/derby/node_modules/racer/lib/refs/index.js:124:19)
at $derbypath/lib/app/index.js:18:11
at Object.fail ($derbypath/node_modules/derby/node_modules/racer/lib/descriptor/descriptor.Model.js:268:21)
at Object.module.exports.events.middleware.middleware.subscribe.add._res.fail ($derbypath/node_modules/derby/node_modules/racer/lib/pubSub/pubSub.Store.js:80:40)
at module.exports.events.init.store.eachContext.context.guardReadPath.context.guardReadPath ($derbypath/node_modules/derby/node_modules/racer/lib/accessControl/accessControl.Store.js:25:26)
at next ($derbypath/node_modules/derby/node_modules/racer/lib/middleware.js:7:26)
at guard ($derbypath/node_modules/derby/node_modules/racer/lib/accessControl/accessControl.Store.js:156:36)
at next ($derbypath/node_modules/derby/node_modules/racer/lib/middleware.js:7:26)
at run ($derbypath/node_modules/derby/node_modules/racer/lib/middleware.js:10:12)
at $derbypath/node_modules/derby/node_modules/racer/lib/pubSub/pubSub.Store.js:88:11
at next ($derbypath/node_modules/derby/node_modules/racer/lib/middleware.js:7:26)
at Object.run [as subscribe] ($derbypath/node_modules/derby/node_modules/racer/lib/middleware.js:10:12)
at module.exports.server._addSub ($derbypath/node_modules/derby/node_modules/racer/lib/descriptor/descriptor.Model.js:237:26)
at Object.Promise.on ($derbypath/node_modules/derby/node_modules/racer/lib/util/Promise.js:29:7)
at Model.module.exports.server._addSub ($derbypath/node_modules/derby/node_modules/racer/lib/descriptor/descriptor.Model.js:221:29)
at subscribe ($derbypath/node_modules/derby/node_modules/racer/lib/descriptor/descriptor.Model.js:267:9)
at Model.module.exports.proto.subscribe ($derbypath/node_modules/derby/node_modules/racer/lib/descriptor/descriptor.Model.js:106:7)
at $derbypath/lib/app/index.js:17:16
at onRoute ($derbypath/node_modules/derby/lib/derby.server.js:69:7)
at app.router ($derbypath/node_modules/derby/node_modules/tracks/lib/index.js:96:16)
at callbacks ($derbypath/node_modules/derby/node_modules/tracks/node_modules/express/lib/router/index.js:160:37)
at param ($derbypath/node_modules/derby/node_modules/tracks/node_modules/express/lib/router/index.js:134:11)
at param ($derbypath/node_modules/derby/node_modules/tracks/node_modules/express/lib/router/index.js:131:11)
at pass ($derbypath/node_modules/derby/node_modules/tracks/node_modules/express/lib/router/index.js:141:5)
at Router._dispatch ($derbypath/node_modules/derby/node_modules/tracks/node_modules/express/lib/router/index.js:169:5)
at dispatch ($derbypath/node_modules/derby/node_modules/tracks/lib/index.js:43:21)
at Object.middleware [as handle] ($derbypath/node_modules/derby/node_modules/tracks/lib/index.js:58:7)
at next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at app.use.fn ($derbypath/node_modules/express/lib/application.js:121:9)
at next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:127:23)
at pass ($derbypath/node_modules/express/lib/router/index.js:107:24)
at Router._dispatch ($derbypath/node_modules/express/lib/router/index.js:170:5)
at Object.router ($derbypath/node_modules/express/lib/router/index.js:33:10)
at Context.next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Context.actions.pass ($derbypath/node_modules/derby-auth/node_modules/passport/lib/passport/context/http/actions.js:77:8)
at SessionStrategy.authenticate ($derbypath/node_modules/derby-auth/node_modules/passport/lib/passport/strategies/session.js:52:10)
at attempt ($derbypath/node_modules/derby-auth/node_modules/passport/lib/passport/middleware/authenticate.js:243:16)
at Passport.authenticate ($derbypath/node_modules/derby-auth/node_modules/passport/lib/passport/middleware/authenticate.js:244:7)
at next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Passport.initialize ($derbypath/node_modules/derby-auth/node_modules/passport/lib/passport/middleware/initialize.js:69:5)
at next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.handle ($derbypath/node_modules/derby-auth/index.js:71:16)
at next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.module.exports [as handle] ($derbypath/node_modules/derby-auth/node_modules/connect-flash/lib/flash.js:20:5)
at next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.expressInit [as handle] ($derbypath/node_modules/express/lib/middleware.js:31:5)
at next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.query [as handle] ($derbypath/node_modules/express/node_modules/connect/lib/middleware/query.js:44:5)
at next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Function.app.handle ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:198:3)
at Object.app.use.fn [as handle] ($derbypath/node_modules/express/lib/application.js:117:11)
at next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.modelMiddleware [as handle] ($derbypath/node_modules/derby/node_modules/racer/lib/session/session.Store.js:119:5)
at next ($derbypath/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at store.get.next ($derbypath/node_modules/derby/node_modules/racer/node_modules/connect/lib/middleware/session.js:313:9)
at $derbypath/node_modules/derby/node_modules/racer/node_modules/connect/lib/middleware/session.js:337:9
at sessStore.load.sessStore.get ($derbypath/node_modules/derby/node_modules/racer/lib/session/session.Store.js:262:11)
at process.startup.processNextTick.process._tickCallback (node.js:245:9)

Check this line at $derbypath/lib/app/index.js:18:11

Related

Elixir issue with Bamboo.SentEmailViewerPlug

I do have an issue with mentioned function. This is the error from browser:
function Bamboo.SentEmailViewerPlug.init/1 is undefined (module Bamboo.SentEmailViewerPlug is not available)
And this is from console :
[error] #PID<0.868.0> running RewardappWeb.Endpoint (connection #PID<0.829.0>, stream id 4) terminated
Server: localhost:4000 (http)
Request: GET /mailbox
** (exit) an exception was raised:
** (UndefinedFunctionError) function Bamboo.SentEmailViewerPlug.init/1 is undefined (module Bamboo.SentEmailViewerPlug is not available)
Bamboo.SentEmailViewerPlug.init([])
(phoenix 1.6.6) lib/phoenix/router/route.ex:41: Phoenix.Router.Route.call/2
(phoenix 1.6.6) lib/phoenix/router.ex:355: Phoenix.Router.__call__/2
(rewardapp 0.1.0) lib/rewardapp_web/endpoint.ex:1: RewardappWeb.Endpoint.plug_builder_call/2
(rewardapp 0.1.0) lib/plug/debugger.ex:136: RewardappWeb.Endpoint."call (overridable 3)"/2
(rewardapp 0.1.0) lib/rewardapp_web/endpoint.ex:1: RewardappWeb.Endpoint.call/2
(phoenix 1.6.6) lib/phoenix/endpoint/cowboy2_handler.ex:54: Phoenix.Endpoint.Cowboy2Handler.init/4
(cowboy 2.9.0) /Users/mateuszosinski/Desktop/elixir/rewardapp/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
(cowboy 2.9.0) /Users/mateuszosinski/Desktop/elixir/rewardapp/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
(cowboy 2.9.0) /Users/mateuszosinski/Desktop/elixir/rewardapp/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
(stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Here is my router.ex file :
defmodule RewardappWeb.Router do
use RewardappWeb, :router
use Phoenix.Router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout, {RewardappWeb.LayoutView, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", RewardappWeb do
pipe_through :browser
#get "/", PageController, :index
#NEW ROUTES
#get "/users", GrantController, :login
get "/admin", GrantController, :admin
get "/main", GrantController, :main
get "/", GrantController, :index
post "/", GrantController, :login
get "/add", GrantController, :add
post "/add/:id", GrantController, :update
get "/admin/delete/:id", GrantController, :delete
end
if Mix.env == :dev do
forward "/mailbox", Bamboo.SentEmailViewerPlug
end
# Other scopes may use custom stacks.
# scope "/api", RewardappWeb do
# pipe_through :api
# end
# Enables LiveDashboard only for development
#
# If you want to use the LiveDashboard in production, you should put
# it behind authentication and allow only admins to access it.
# If your application does not have an admins-only section yet,
# you can use Plug.BasicAuth to set up some basic authentication
# as long as you are also using SSL (which you should anyway).
if Mix.env() in [:dev, :test] do
import Phoenix.LiveDashboard.Router
scope "/" do
pipe_through :browser
live_dashboard "/dashboard", metrics: RewardappWeb.Telemetry
end
end
# Enables the Swoosh mailbox preview in development.
#
# Note that preview only shows emails that were sent by the same
# node running the Phoenix server.
if Mix.env() == :dev do
scope "/dev" do
pipe_through :browser
#forward "/mailbox", Plug.Swoosh.MailboxPreview
end
end
end
Mails are being sent totally fine - I have inspected that with IO.inspect, and right now, they are sent fine. However, I can not attempt to see localhost:4000/mailbox. Where is my mistake? Thank you in advance!!
The issue was with wrong name Bamboo module name.

How to get and use Rundeck JSESSIONID

I am trying to get the a valid session id but so far with no success.
I am trying to work with the rundeck API, with password authentication:
To submit authentication, submit a POST request to the URL:
$RUNDECK_SERVER_URL/j_security_check
With these parameters:
j_username: rundeck username j_password: password
Like the docs suggest, I am running:
>>> import request
>>> jsessionid = requests.post("http://localhost:4440/j_security_check", data=
{"j_username": "admin", "j_password": "admin" },
allow_redirects=False).cookies['JSESSIONID']
Resulting the log:
09/04/2017 03:22:59 PM - DEBUG - Starting new HTTP connection (1):
10.200.101.21
09/04/2017 03:22:59 PM - DEBUG - http://localhost:4440 "POST
/j_security_check?j_username=admin&j_password=admin HTTP/1.1" 302 0
But the jsessionid is never valid for the next api calls, for example:
>>> response = requests.post("http://localhost:4440/api/14/project/my_project/hi
story" , headers={ "Content-Type": 'application/xml', 'X-Rundeck-
Auth-Token': jsessionid })
>>> response.content
"<result error='true' apiversion='20'><error code='unauthorized'>
<message>(Token:lhefo****) is not authorized for: /api/14/project/weekly_builds/history</message></error></result>"
However, if for jsessionid I am using the token I can see in the Rundeck UI (in http://localhost:4440/user/profile), then the API call works good and I get the history.
Any ideas of what I am doing wrong? Or what I am missing?
Thanks.
Don't worry about the JSESSIONID. You should use requests.Seesion()
import requests
s = requests.Session()
r = s.post("URL:PORT/j_security_check", data={"j_username": "user", "j_password": "pass"})
r.status_code # this should be 200
r.url # this should be rundeck home
r = s.get("URL:PORT/api/11/projects",headers = {'Accept': 'application/json'})
r.json() # you should see all your project

How to use gspread with python-social-auth

I'm trying to use gspread with python-social-auth, I followed the sample describe on documentation and I've created this class to use as credential store:
class Credentials(object):
def __init__ (self, access_token=None, scope=None):
self.access_token = access_token
self.scope=scope
def refresh (self, http):
# get new access_token
# this only gets called if access_token is None
pass
And at my code I've used:
import gspread
credentials = Credentials(access_token=user.social_auth.get_access_token(),
scope=['https://spreadsheets.google.com/feeds'])
sh = gspread.authorize(credentials)
And asking any response from API using:
sh.get_spreadsheets_feed()
This error appears on the console:
*** RequestError: (401, '401: <HTML>\n<HEAD>\n<TITLE>Token invalid - AuthSub token has wrong scope</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>Token invalid - AuthSub token has wrong scope</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n')
I have defined the scope at my settings and this is working well, for example trying to get contacts from Google Contacts
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
'https://www.googleapis.com/auth/contacts.readonly',
'https://www.googleapis.com/auth/spreadsheets.readonly'
]
Any ideas?
You have to add these scopes also to Python Social Auth settings:
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
...
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive.file'
]

How can I check the connection of Mongoid

Does Mongoid has any method like ActiveRecord::Base.connected??
I want to check if the connection that's accessible.
We wanted to implement a health check for our running Mongoid client that tells us whether the established connection is still alive. This is what we came up with:
Mongoid.default_client.database_names.present?
Basically it takes your current client and tries to query the databases on its connected server. If this server is down, you will run into a timeout, which you can catch.
My solution:
def check_mongoid_connection
mongoid_config = File.read("#{Rails.root}/config/mongoid.yml")
config = YAML.load(mongoid_config)[Rails.env].symbolize_keys
host, db_name, user_name, password = config[:host], config[:database], config[:username], config[:password]
port = config[:port] || Mongo::Connection::DEFAULT_PORT
db_connection = Mongo::Connection.new(host, port).db(db_name)
db_connection.authenticate(user_name, password) unless (user_name.nil? || password.nil?)
db_connection.collection_names
return { status: :ok }
rescue Exception => e
return { status: :error, data: { message: e.to_s } }
end
snrlx's answer is great.
I use following in my puma config file, FYI:
before_fork do
begin
# load configuration
Mongoid.load!(File.expand_path('../../mongoid.yml', __dir__), :development)
fail('Default client db check failed, is db connective?') unless Mongoid.default_client.database_names.present?
rescue => exception
# raise runtime error
fail("connect to database failed: #{exception.message}")
end
end
One thing to remind is the default server_selection_timeout is 30 seconds, which is too long for db status check at least in development, you can modify this in your mongoid.yml.

500 internal server error on certain page after a few hours

I am getting a 500 Internal Server Error on a certain page of my site after a few hours of being up. I restart uWSGI instance with uwsgi --ini /home/metheuser/webapps/ers_portal/ers_portal_uwsgi.ini and it works again for a few hours.
The rest of the site seems to be working. When I navigate to my_table, I am directed to the login page. But, I get the 500 error on my table page on login. I followed the instructions here to set up my nginx and uwsgi configs.
That is, I have ers_portal_nginx.conf located i my app folder that is symlinked to /etc/nginx/conf.d/. I start my uWSGI "instance" (not sure what exactly to call it) in a Screen instance as mentioned above, with the .ini file located in my app folder
My ers_portal_nginx.conf:
server {
listen 80;
server_name www.mydomain.com;
location / { try_files $uri #app; }
location #app {
include uwsgi_params;
uwsgi_pass unix:/home/metheuser/webapps/ers_portal/run_web_uwsgi.sock;
}
}
My ers_portal_uwsgi.ini:
[uwsgi]
#user info
uid = metheuser
gid = ers_group
#application's base folder
base = /home/metheuser/webapps/ers_portal
#python module to import
app = run_web
module = %(app)
home = %(base)/ers_portal_venv
pythonpath = %(base)
#socket file's location
socket = /home/metheuser/webapps/ers_portal/%n.sock
#permissions for the socket file
chmod-socket = 666
#uwsgi varible only, does not relate to your flask application
callable = app
#location of log files
logto = /home/metheuser/webapps/ers_portal/logs/%n.log
Relevant parts of my views.py
data_modification_time = None
data = None
def reload_data():
global data_modification_time, data, sites, column_names
filename = '/home/metheuser/webapps/ers_portal/app/static/' + ec.dd_filename
mtime = os.stat(filename).st_mtime
if data_modification_time != mtime:
data_modification_time = mtime
with open(filename) as f:
data = pickle.load(f)
return data
#a bunch of authentication stuff...
#app.route('/')
#app.route('/index')
def index():
return render_template("index.html",
title = 'Main',)
#app.route('/login', methods = ['GET', 'POST'])
def login():
login stuff...
#app.route('/my_table')
#login_required
def my_table():
print 'trying to access data table...'
data = reload_data()
return render_template("my_table.html",
title = "Rundata Viewer",
sts = sites,
cn = column_names,
data = data) # dictionary of data
I installed nginx via yum as described here (yesterday)
I am using uWSGI installed in my venv via pip
I am on CentOS 6
My uwsgi log shows:
Wed Jun 11 17:20:01 2014 - uwsgi_response_writev_headers_and_body_do(): Broken pipe [core/writer.c line 287] during GET /whm-server-status (127.0.0.1)
IOError: write error
[pid: 9586|app: 0|req: 135/135] 127.0.0.1 () {24 vars in 292 bytes} [Wed Jun 11 17:20:01 2014] GET /whm-server-status => generated 0 bytes in 3 msecs (HTTP/1.0 404) 2 headers in 0 bytes (0 switches on core 0)
When its working, the print statement in the views "my_table" route prints into the log file. But not once it stops working.
Any ideas?