I'm new to Riemann and also new to ruby and Clojure as well. I'm trying to output the internal riemann events via (streams prn) in my riemann.config file. I currently see messages being printed out in the terminal from where I launched riemann.
Ex:
#riemann.codec.Event{:host "localhost.localdomain", :service "riemann server ws 0.0.0.0:5556 in latency 0.999", :state "ok", :description nil, :metric nil, :tags nil, :time 283857867047/200, :ttl 20}
In my dashboard however I'm unable to get these to print to any sort of log or gauge.
I tried using the following as a service =~ "riemann %" from here
I get an orange message displaying 1 socket errors; check the server field above or a large question market above the title.
Not sure what else to try or do from here to identify what went wrong.
The riemann dashboard displays events in the index, which prn does not update. Use (streams index) to capture the events for the dashboard.
Related
I'd like to get OpenID connect working in my little luminus project. I'm a little new to the workflow in luminus/ring/compojure (coming from django, flask, and servlets mostly). I've successfully redirected to Google so I get the "code" back from Google, but then I need to make one more request to Google before logging in the user and this call requires another callback the user is not involved in, so I need to put the user's request on hold like a promise, but I'm not sure how that part works in compojure.
; this is my code that redirects them to Google, where they accept
(defn login [params]
(let [google-oauth2-client-id (System/getenv "GOOGLE_OAUTH2_CLIENT_ID")
base-url "https://accounts.google.com/o/oauth2/auth"
args {"client_id" google-oauth2-client-id
"response_type" "code"
"scope" "openid email"
"redirect_uri" "http://localhost:3000/oauth2Callback"
"state" "anti-forgery here"}]
(assert google-oauth2-client-id "can't find GOOGLE_OAUTH2_CLIENT_ID in environment")
(redirect (str base-url "?" (make-query-string args)))
)
)
; this is my code handling Google's first response
(defn oauth2-callback [params]
; params has the code to send to Google
; here I should send another request to google that comes back to another callback like oauth2-token-callback that processes the request to the user in the current context
(redirect "/youreloggedin")
)
By the end of this method I should be sending the user a message saying they're logged in, but I need to wait until the request comes back. How is this workflow handled in luminus?
Solved. I didn't realize I could just ignore the callback parameter.
(client/post "https://www.googleapis.com/oauth2/v3/token"
{:headers {"X-Api-Version" "2"}
:content-type :application/x-www-form-urlencoded
:form-params {:code (params :code)
:client_id (System/getenv "GOOGLE_OAUTH2_CLIENT_ID")
:client_secret (System/getenv "GOOGLE_OAUTH2_CLIENT_SECRET")
:redirect_uri "http://localhost:3000/oauth2Callback" ; ignored
:grant_type "authorization_code"
}
:as :auto ; decode the body straight to hash (if possible)
})
Based on the documentation for Google's OAuth2 for web servers here, the flow consists of the following steps:
Your application redirects a browser to a Google URL; the URL includes query parameters that indicate the type of access being requested.
The result is an authorization code, which Google returns to your application in a query string.
After receiving the authorization code, your application can exchange the code (along with a client ID and client secret) for an access token and, in some cases, a refresh token.
If I understood your question correctly, step 3 does not necessarily involve a callback to your server, you can just perform the request to Google with an HTTP client. I recently implemented OAuth2 for GitHub in this project, step 3 is implemented in this function:
(defn callback
"Handles the callback from GitHub OAuth flow."
[code]
(let [params {:form-params {:client_id client-id
:client_secret client-secret
:code code}}
{:keys [body]} (client/post access-token-url params) ;; This is doing the POST
;; request to GitHub.
token ((qs-map body) "access_token")] ;; Getting the token from
;; the response here.
{:status 302
:headers {"location" "/repos"
"set-cookie" (str "token=" token ";Path=/")}}))
I used clj-http as the HTTP client but any other will do.
I'm working on a clojure web application project for my college,and i am trying to connect datomic database with friend authentication but is kinda buggy...i will explain further...
First i am doing user registration(user insertion in datomic database) like this and it is working.
(defn insert-user [firstname lastname email password sex date] (.get (.transact conn
[{
:db/id #db/id[:db.part/user -1000001]
:user/name firstname
:user/lastName lastname
:user/username email
:user/password (creds/hash-bcrypt password)
:user/gender sex
:user/birthDate date}
]))
(resp/redirect "/")
)
The routes handler and friend authenticator looks like this...function main is to start the app.
(def page (handler/site
(friend/authenticate
routes
{:allow-anon? true
:login-uri "/login"
:default-landing-uri "/login"
:unauthorized-handler #(-> (html5 [:h2 "You do not have sufficient privileges to access " (:uri %)])
resp/response
(resp/status 401))
:credential-fn (partial creds/bcrypt-credential-fn users)
:workflows [(workflows/interactive-form)]})
(wrap-keyword-params routes)
(wrap-nested-params routes)
(wrap-params routes)
))
(defn -main []
(run-jetty page {:port 8080 :join? false}))
And for the end the datomic query for users to match with creds/bcrypt-credential-fn function of friend.
(defn upit-korisnici []
(def temp (d/q '[:find ?u ?p
:where [?user :user/username ?u]
[?user :user/password ?p]
]
(d/db conn)))
(def users (into {} (map (fn [[k v]] [k {:username k :password v}]) temp)))
users
)
The thing that is bugging me and leaving me helpless is that when i register(insert user),the user is inserted in datomic database but when i try to log in i can't.It says wrong email and password but the new user is there.When i restart the whole app and try to login with the new users credentials it goes through and logs on.Does anyone know how to solve this problem?
Edit:
I solved this problem by changing :credential-fn (partial creds/bcrypt-credential-fn users) to :credential-fn #(creds/bcrypt-credential-fn users %).
You seem to think it will automatically update your user data, but it isn't going to because user is not a function it is plain data. What happens is (def user... is run then the results are bound to the name user, you are not binding the computation so the data is never updated. You make a similar mistake for temp. The query is run once then the results are bound to temp, and never reevaluated. You should bind them to a function so that it revaluates.
I started working on an UI for the friend lib with datomic persistence: https://github.com/sveri/friend-ui/ You can have a look at it, maybe it solves your problem already, of course you can take code from it, add pull requests / whatever. When I have time I will implement whatever is needed.
Currently it supports:
Sign up
Login
Logout
Twitter Bootstrap support for the templates
It would be nice if we could bundle the work done, as this will be something that many people will need in future.
I am writing a small website in Clojure and Compojure. I would like to set the HTTP response status for each request based on the data found or not found.
The last call is the html5 macro that returns to the handler the html that needs to be sent back to the browser. Is it possible to set the HTTP response status somehow here?
(ns myapp.views.layout
(:require
[hiccup.page :refer (html5 include-css include-js)]))
(defn layout [title & content]
(html5
(head title)
(body content)))
If you only return text that text will be the body of the response. If you return a map, the map can describe other aspects of the response.
(defn layout [title & content]
{:status 200
:body (html5 (head title) (body content))})
If you return a map containing
{:status NNN
:body (my-code-here)}
then the contents of the :status key will be the http response code.
Just to add some details that might be helpful or interesting to others, each of the return values of your Compojure route handlers "... is treated intelligently" and that intelligence is encapsulated in the "compojure.response/render multimethod".
Based on a cursory examination of the render code, the reason why returning a map works is that the map you return is merge-d with the Ring response map that Compojure implicitly creates.
You might also want to include :headers {"Content-Type" "text/html"} (or whatever's appropriate) in the map for your handler return values. A Unicode character in the page title in my responses wasn't being rendered correctly because the content type header was missing.
When I click the search button on this page, it sends a post request. I want to do the post via cli-http. How can I do that?
(def default-http-opts
{:socket-timeout 10000
:conn-timeout 10000
:insecure? true
:throw-entire-message? false})
(clj-http/post initial-url default-http-opts)
can post a request but the problem is that I want to pass in some parameters. These parameters(the buttons selected) are default on the page.
They are:
AdvancedSearchForm:CourseOrSubjectSelection=ALL_ALL
AdvancedSearchForm:GraduateLevelSelection=ALL
AdvancedSearchForm:allStudyAreas=t
AdvancedSearchForm:departmentList=
AdvancedSearchForm:facultyList=
AdvancedSearchForm:keywords=
AdvancedSearchForm:level=ALL
AdvancedSearchForm:semester=ALL
oracle.adf.faces.FORM=AdvancedSearchForm
oracle.adf.faces.STATE_TOKEN=_id21519:_id21520
source=AdvancedSearchForm:searchButton
The key AdvancedSearchForm:semester contains ':', so I use string as a key like this "AdvancedSearchForm:semester", is it OK in clj-http?
I do it like this:
(spit (file "/tmp" "ts.html")
(:body (http/post initial-url
{:form-params {"AdvancedSearchForm:CourseOrSubjectSelection" "ALL_ALL", "AdvancedSearchForm:GraduateLevelSelection" "ALL"}})))`
Actually the page it returns is indeed "Results" but no courses are listed. only the template. I want to get all the course links which are only shown by manually click. Any help?
is the image I screenshot from Tamper Data. It shows what happens after I click the Search button. Seems like client is redirected to searchresult.jsp. I use curl to imitate that. I do it like this
curl -D "form data..." https://handbook.unimelb.edu.au/faces/htdocs/user/search/AdvancedSearch.jsp
Then quickly run
curl https://handbook.unimelb.edu.au/faces/htdocs/user/search/SearchResults.jsp
No results contents are shown though the page is downloaded.
It looks like the server doesn't understand the parameters you send to it.
The escaping in use is the percent-encoding. Try to check out if it get in use by using the debug functionality avail in clj-https README.md:
;; print request info to *out*, including request body:
(client/post "http://example.org" {:debug true :debug-body true :body "..."})
or try to manually run the requests either with the curl command in a terminal or with the convenient Firefox restclient add-on.
From their GitHub page (https://github.com/dakrone/clj-http):
;; Send form params as a urlencoded body (POST or PUT)
(client/post "http//site.com" {:form-params {:foo "bar"}})
I'm trying to send email from Clojure using the following code:
Helper function that sends email:
(defn- send [recipient from subject msg host content-type & attachments]
(let [att-ds (map (fn [at] {:ds (ByteArrayDataSource. (:source at)
(:type at))
:name (:name at)
:description (:description at)})
attachments)
mpmail (MultiPartEmail.)]
(doto mpmail
(.setHostName host)
(.setFrom from)
(.setSubject subject)
(.setContent msg content-type)
(.setCharset "utf-8"))
(.addTo mpmail recipient)
(doseq [ds att-ds]
(.attach mpmail (:ds ds) (:name ds) (:description ds)))
(.send mpmail)))
Usage:
(send "sender#my.domain"
"recipient#my.domain"
"SUBJECT"
"MSG"
"my.smtp.server"
"text/plain"
{:source (.getBytes "Attachment")
:type "text/plain"
:name "test.txt"
:description "test"})
Running the above from the REPL (or from my app) results in recipient#my.domain receiving an email with the subject "SUBJECT" and body "MSG" but without any traces of attachment. No exceptions are raised anywhere.
I have tried this with two different smtp servers.
Thanks for any help.
Try replace (.setContent msg) with (.setMsg msg). May be when you call setContent it thinks you manually set content and ignores following attach methods.
Try to use my code snippet http://middlesphere-1.blogspot.ru/2014/11/clojure-how-to-send-mail-with-attachment.html
attachment filename can be in unicode.